mirror of
https://github.com/eggjs/egg.git
synced 2024-12-04 07:14:30 +00:00
feat(tsd): add ctx.logger and logger.error support Error object (#1108)
This commit is contained in:
parent
7c2e43626d
commit
daa8227833
11
index.d.ts
vendored
11
index.d.ts
vendored
@ -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
|
||||
|
||||
15
test/fixtures/apps/app-ts/app/controller/foo.ts
vendored
15
test/fixtures/apps/app-ts/app/controller/foo.ts
vendored
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user