Skip to content

Commit c00e2db

Browse files
committed
feat: add expressErrorCallback
AdminForth/1781/image
1 parent 04d8b63 commit c00e2db

2 files changed

Lines changed: 57 additions & 2 deletions

File tree

adminforth/servers/express.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,8 +710,29 @@ class ExpressServer implements IExpressHttpServer {
710710
output = await handler(input);
711711
} catch (e) {
712712
afLogger.error(`Error in handler ${e}`);
713-
// print full stack trace
713+
// print full stack trace
714714
afLogger.error(e.stack);
715+
const expressErrorCallback = this.adminforth.config.expressErrorCallback;
716+
if (expressErrorCallback) {
717+
try {
718+
await expressErrorCallback({
719+
error: e,
720+
adminforth: this.adminforth,
721+
extra: {
722+
body,
723+
query,
724+
headers,
725+
cookies: cookies as any,
726+
requestUrl,
727+
meta: {},
728+
response,
729+
},
730+
});
731+
} catch (callbackError) {
732+
afLogger.error(`Error in expressErrorCallback ${callbackError}`);
733+
afLogger.error(callbackError?.stack);
734+
}
735+
}
715736
res.status(500).send('Internal server error');
716737
return;
717738
}

adminforth/types/Back.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,28 @@ export type AdminUserAuthorizeFunction = ((params?: {
12521252
allowed?: boolean,
12531253
}>);
12541254

1255-
1255+
1256+
/**
1257+
* Callback called when an unhandled error is thrown inside an AdminForth API endpoint handler.
1258+
* Useful to report errors to external services (e.g. Sentry) or to do custom logging.
1259+
* Thrown errors / rejections from this callback are caught and logged, they do not affect the response.
1260+
*/
1261+
export type ExpressErrorCallbackFunction = ((params: {
1262+
/**
1263+
* The error thrown by the endpoint handler.
1264+
*/
1265+
error: any,
1266+
/**
1267+
* Adminforth instance.
1268+
*/
1269+
adminforth: IAdminForth,
1270+
/**
1271+
* Extra HTTP information about the request which caused the error.
1272+
*/
1273+
extra: HttpExtra,
1274+
}) => void | Promise<void>);
1275+
1276+
12561277
/**
12571278
* Data source describes database connection which will be used to fetch data for resources.
12581279
* Each resource should use one data source.
@@ -1844,6 +1865,19 @@ export interface AdminForthInputConfig {
18441865
* List of plugins that should be applied in global scope.
18451866
*/
18461867
globalPlugins?: Array<IAdminForthPlugin>,
1868+
1869+
/**
1870+
* Callback executed when an unhandled error is thrown inside an AdminForth API endpoint handler.
1871+
* Receives the thrown error and extra HTTP information about the request.
1872+
* Useful to report errors to external services (e.g. Sentry) or to do custom logging.
1873+
*
1874+
* ```ts
1875+
* expressErrorCallback: ({ error, extra }) => {
1876+
* Sentry.captureException(error, { extra: { url: extra.requestUrl } });
1877+
* },
1878+
* ```
1879+
*/
1880+
expressErrorCallback?: ExpressErrorCallbackFunction,
18471881
}
18481882

18491883

0 commit comments

Comments
 (0)