Skip to content

Commit

Permalink
feat: add context to error handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-tkachenko committed Dec 11, 2023
1 parent b22fdfc commit 8260efa
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/Prxi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export class Prxi {
})
.catch((err: Error) => {
this.logError(`[${requestId}] [Prxi] Error occurred upon making the "${req.method}:${path}" request`, err);
errHandler(req, res, err)
errHandler(req, res, err, context)
.catch(err => {
this.logError(`[${requestId}] [Prxi] Unable to handle error with errorHandler`, err);
this.send500Error(req, res);
Expand All @@ -205,7 +205,7 @@ export class Prxi {
/* istanbul ignore next */
this.configuration.on?.afterHTTPRequest?.(req, res, context);
this.logError(`[${requestId}] [Prxi] Missing RequestHandler configuration for the "${req.method}:${path}" request`);
errorHandler(req, res, new Error(`Missing RequestHandler configuration for the "${req.method}:${path}" request`))
errorHandler(req, res, new Error(`Missing RequestHandler configuration for the "${req.method}:${path}" request`), context)
.catch(err => {
this.logError(`[${requestId}] [Prxi] Unable to handle error with errorHandler`, err);
this.send500Error(req, res);
Expand Down Expand Up @@ -274,7 +274,7 @@ export class Prxi {
})
.catch((err: Error) => {
this.logError(`[${requestId}] [Prxi] Error occurred upon making the "${method}:${path}" request`, err);
http2ErrHandler(stream, headers, err)
http2ErrHandler(stream, headers, err, context)
.catch(err => {
this.logError(`[${requestId}] [Prxi] Unable to handle error with errorHandler`, err);
this.send500ErrorForHttp2(stream, headers);
Expand All @@ -284,7 +284,7 @@ export class Prxi {
/* istanbul ignore next */
this.configuration.on?.afterHTTP2Request?.(stream, headers, context);
this.logError(`[${requestId}] [Prxi] Missing RequestHandler configuration for the "${method}:${path}" HTTP/2 request`);
http2ErrorHandler(stream, headers, new Error(`Missing RequestHandler configuration for the "${method}:${path}" HTTP/2 request`))
http2ErrorHandler(stream, headers, new Error(`Missing RequestHandler configuration for the "${method}:${path}" HTTP/2 request`), context)
.catch(err => {
this.logError(`[${requestId}] [Prxi] Unable to handle error with errorHandler`, err);
this.send500ErrorForHttp2(stream, headers);
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { SecureContextOptions } from 'node:tls';
import { Stream } from 'node:stream';
import { Socket } from 'node:net';

export type ErrorHandler = (req: Request, res: Response, err: Error) => Promise<void>;
export type Http2ErrorHandler = (stream: ServerHttp2Stream, headers: IncomingHttpHeaders, err: Error) => Promise<void>;
export type ErrorHandler = (req: Request, res: Response, err: Error, context: Record<string, any>) => Promise<void>;
export type Http2ErrorHandler = (stream: ServerHttp2Stream, headers: IncomingHttpHeaders, err: Error, context: Record<string, any>) => Promise<void>;

export interface Configuration {
/**
Expand Down

0 comments on commit 8260efa

Please sign in to comment.