Skip to content

Commit 8dee375

Browse files
authored
Merge pull request #697 from devforth/next
feat: add expressErrorCallback
2 parents 531ee88 + c00e2db commit 8dee375

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
@@ -717,8 +717,29 @@ class ExpressServer implements IExpressHttpServer {
717717
output = await handler(input);
718718
} catch (e) {
719719
afLogger.error(`Error in handler ${e}`);
720-
// print full stack trace
720+
// print full stack trace
721721
afLogger.error(e.stack);
722+
const expressErrorCallback = this.adminforth.config.expressErrorCallback;
723+
if (expressErrorCallback) {
724+
try {
725+
await expressErrorCallback({
726+
error: e,
727+
adminforth: this.adminforth,
728+
extra: {
729+
body,
730+
query,
731+
headers,
732+
cookies: cookies as any,
733+
requestUrl,
734+
meta: {},
735+
response,
736+
},
737+
});
738+
} catch (callbackError) {
739+
afLogger.error(`Error in expressErrorCallback ${callbackError}`);
740+
afLogger.error(callbackError?.stack);
741+
}
742+
}
722743
res.status(500).send('Internal server error');
723744
return;
724745
}

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)