Skip to content

Commit

Permalink
fix(api): fix body parsing in sentry reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
Redm4x committed Sep 4, 2024
1 parent bfca9b5 commit df86495
Showing 1 changed file with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,20 @@ export class HonoErrorHandlerService {
}

private async reportError<E extends Env = any>(error: Error, c: Context<E>): Promise<void> {

Check warning on line 41 in apps/api/src/core/services/hono-error-handler/hono-error-handler.service.ts

View workflow job for this annotation

GitHub Actions / validate-n-build

Unexpected any. Specify a different type
const id = this.sentry.captureEvent(await this.getSentryEvent(error, c));
this.logger.info({ event: "SENTRY_EVENT_REPORTED", id });
try {
const id = this.sentry.captureEvent(await this.getSentryEvent(error, c));
this.logger.info({ event: "SENTRY_EVENT_REPORTED", id });
} catch (e) {
this.logger.error(e);
}
}

private async getSentryEvent<E extends Env = any>(error: Error, c: Context<E>): Promise<Event> {

Check warning on line 50 in apps/api/src/core/services/hono-error-handler/hono-error-handler.service.ts

View workflow job for this annotation

GitHub Actions / validate-n-build

Unexpected any. Specify a different type
const event = this.sentry.addRequestDataToEvent(this.sentryEventService.toEvent(error), {
method: c.req.method,
url: c.req.url,
headers: omit(Object.fromEntries(c.req.raw.headers), ["x-anonymous-user-id"]),
body: await c.req.json()
body: await this.getSentryEventRequestBody(c)
});
const currentSpan = trace.getSpan(context.active());

Expand All @@ -65,4 +69,18 @@ export class HonoErrorHandlerService {

return event;
}

private async getSentryEventRequestBody<E extends Env = any>(c: Context<E>) {

Check warning on line 73 in apps/api/src/core/services/hono-error-handler/hono-error-handler.service.ts

View workflow job for this annotation

GitHub Actions / validate-n-build

Unexpected any. Specify a different type
switch (c.req.header("content-type")) {
case "text/plain":
return await c.req.text();
case "application/json":
return await c.req.json();
case "application/x-www-form-urlencoded":
case "multipart/form-data":
return await c.req.parseBody();
default:
return undefined;
}
}
}

0 comments on commit df86495

Please sign in to comment.