feat(tsd): add ctx.logger and logger.error support Error object (#1108)

This commit is contained in:
Eward Song 2017-06-26 21:54:45 +08:00 committed by Haoliang Gao
parent 7c2e43626d
commit daa8227833
2 changed files with 22 additions and 4 deletions

11
index.d.ts vendored
View File

@ -35,9 +35,11 @@ export interface Logger {
info(info: string, ...args: string[]): void;
warn(info: string, ...args: string[]): void;
debug(info: string, ...args: string[]): void;
error(info: string, ...args: string[]): void;
error(info: string | Error, ...args: string[]): void;
}
export type RequestArrayBody = any[];
export type RequestObjectBody = { [key: string]: any };
interface Request extends KoaApplication.Request { // tslint:disable-line
/**
* detect if response should be json
@ -116,6 +118,8 @@ interface Request extends KoaApplication.Request { // tslint:disable-line
* ```
*/
query: { [key: string]: string };
body: RequestArrayBody | RequestObjectBody;
}
interface Response extends KoaApplication.Response { // tslint:disable-line
@ -724,6 +728,11 @@ export interface Context extends KoaApplication.Context {
*/
curl(url: string, opt: any): Promise<any>;
/**
* Get logger by name, it's equal to app.loggers['name'], but you can extend it with your own logical
*/
getLogger(name: string): Logger;
/**
* Render a file by view engine
* @param {String} name - the file path based on root

View File

@ -1,4 +1,4 @@
import { Controller } from 'egg';
import { Controller, RequestObjectBody } from 'egg';
// add user controller and service
declare module 'egg' {
@ -10,9 +10,18 @@ declare module 'egg' {
// controller
export default class FooController extends Controller {
async getData() {
this.ctx.body = await this.ctx.service.foo.bar();
try {
this.ctx.body = await this.ctx.service.foo.bar();
} catch (e) {
const body: RequestObjectBody = this.ctx.request.body;
this.app.logger.info(e.name, body.foo);
}
}
async getBar() {
this.ctx.body = await this.service.foo.bar();
try {
this.ctx.body = await this.service.foo.bar();
} catch (e) {
this.ctx.logger.error(e);
}
}
}