Skip to content

feat(logger): log internal server errors #429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Logger } from "./logger";

export class Config {
public logger: Logger = console;
}
10 changes: 8 additions & 2 deletions src/handler/APIGatewayProxyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ValidationError,
UnauthorizedError,
} from "../error";
import { UnprocessableEntityError } from "../error/UnprocessableEntityError";
import { UnprocessableEntityError } from "../error";
import { ContentTypeHeader, CORSHeader, Header, Headers } from "../header";
import {
badRequest,
Expand All @@ -23,6 +23,7 @@ import {
unprocessableEntity,
} from "../response";
import { BaseHandler, BaseHandlerArguments } from "./BaseHandler";
import { config } from "../index";

export interface APIGatewayProxyHandlerArguments extends BaseHandlerArguments {
cors?: CORSHeader;
Expand All @@ -48,10 +49,15 @@ export class APIGatewayProxyHandler extends BaseHandler {
if (err instanceof NotFoundError) {
return notFound(err.details);
}
config.logger.error({
name: err.name,
message: err.message,
stack: err.stack,
});
return internalServerError();
}

private corsHeader: Header;
private readonly corsHeader: Header;

constructor(args?: APIGatewayProxyHandlerArguments) {
super(args);
Expand Down
8 changes: 7 additions & 1 deletion src/handler/BaseHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ import {
import { Format } from "../format/Format";
import * as inputFormat from "../format/InputFormat";
import * as outputFormat from "../format/OutputFormat";
import { Logger } from "../logger";
import { config } from "../index";

export interface BaseHandlerArguments {
inputFormat?: Format;
outputFormat?: Format;
logger?: Logger;
}

type Event =
Expand Down Expand Up @@ -84,9 +87,12 @@ export abstract class BaseHandler {
protected inputFormat: Format;
protected outputFormat: Format;

constructor(args?: BaseHandlerArguments) {
protected constructor(args?: BaseHandlerArguments) {
this.inputFormat = args?.inputFormat ?? inputFormat.json;
this.outputFormat = args?.outputFormat ?? outputFormat.json;
if (args?.logger) {
config.logger = args.logger;
}
}

public decorator(
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Config } from "./Config";

export * from "./decorator/format";
export * from "./decorator/handler";
export * from "./decorator/header";
Expand All @@ -6,6 +8,8 @@ export * from "./error";
import * as input from "./format/InputFormat";
import * as output from "./format/OutputFormat";

export const config = new Config();

export const format = {
input,
output,
Expand Down
9 changes: 9 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type JSONType = { [key: string]: unknown };
export type LoggerArgument = (JSONType | string | unknown)[];

export interface Logger {
info(...args: LoggerArgument): void;
warn(...args: LoggerArgument): void;
error(...args: LoggerArgument): void;
debug(...args: LoggerArgument): void;
}