Skip to content

Commit

Permalink
fix: Error request always return 200 after being sent to sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien2p committed Oct 17, 2022
1 parent e71f7f2 commit c37272a
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 21 deletions.
2 changes: 0 additions & 2 deletions packages/medusa-plugin-sentry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ Then, go to your `medusa-config.js` file and in the plugins collection property
];
},
tracesSampleRate: 1.0,
shouldHandleError: (code) => code >= 400,
webHookOptions: {
path: "/sentry/webhook",
secret: "__YOUR_SECRET__",
Expand Down Expand Up @@ -74,7 +73,6 @@ export type SentryWebHookOptions = {

export type SentryOptions = Omit<NodeOptions, 'integrations'> & {
integrations: Integration[] | ((router: Router, sentry: typeof Sentry, tracing: typeof Tracing) => Integration[]);
shouldHandleError: (code: number) => boolean;
requestHandlerOptions?: RequestHandlerOptions;
enableRequestHandler?: boolean;
enableTracing?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/medusa-plugin-sentry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"scripts": {
"build": "run-s clean build:tsc",
"build:tsc": "tsc -b",
"clean": "rimraf api handlers utils types coverage tsconfig.tsbuildinfo",
"clean": "rimraf api services utils types coverage tsconfig.tsbuildinfo",
"test": "jest",
"test:ci": "yarn add @medusajs/medusa@${MEDUSAJS_VERSION} && yarn run test"
},
Expand Down
18 changes: 4 additions & 14 deletions packages/medusa-plugin-sentry/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export default function (rootDirectory, pluginOptions: SentryOptions): Router {

const {
integrations,
shouldHandleError = (code) => code >= 400,
requestHandlerOptions = {},
enableTracing = true,
enableRequestHandler = true,
Expand All @@ -40,7 +39,7 @@ export default function (rootDirectory, pluginOptions: SentryOptions): Router {
router.use(Sentry.Handlers.tracingHandler());
}

attachSentryErrorHandler(shouldHandleError);
attachSentryErrorHandler();

if (webHookOptions) {
attachSentryWebHook(router, webHookOptions);
Expand All @@ -53,26 +52,17 @@ export default function (rootDirectory, pluginOptions: SentryOptions): Router {

/**
* Attach the sentry error handler in the medusa core
* @param shouldHandleError
*/
function attachSentryErrorHandler(shouldHandleError) {
function attachSentryErrorHandler() {
/* eslint-disable @typescript-eslint/no-var-requires */
const medusaErrorHandler = require('@medusajs/medusa/dist/api/middlewares/error-handler');
const originalMedusaErrorHandler = medusaErrorHandler.default;
medusaErrorHandler.default = () => {
return (err, req, res, next) => {
let statusCode;
const res_ = {
...res,
status: (status) => {
statusCode = status;
return res;
},
};
originalMedusaErrorHandler()(err, req, res_, next);
Sentry.Handlers.errorHandler({
shouldHandleError: () => shouldHandleError(statusCode),
shouldHandleError: () => true,
})(err, req, res, () => void 0);
originalMedusaErrorHandler()(err, req, res, next);
};
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ describe('sentry service', () => {

beforeAll(() => {
axiosGetSpy = jest.spyOn(axios, 'get').mockImplementation(() => {
return Promise.resolve({ data: { data: [], meta: {} }, headers: { link: 'results="true";cursor="0:100:0"' } });
return Promise.resolve({
data: { data: [], meta: {} },
headers: { link: 'results="true";cursor="0:100:0"' },
});
});

sentryService = new SentryService(
Expand Down
4 changes: 2 additions & 2 deletions packages/medusa-plugin-sentry/src/services/sentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default class SentryService extends TransactionBaseService {
cursor,
}: {
organisation: string;
project: string
project: string;
token: string;
query?: string;
statsPeriod?: string;
Expand Down Expand Up @@ -111,7 +111,7 @@ export default class SentryService extends TransactionBaseService {
}: {
transaction: string;
organisation: string;
project: string
project: string;
token: string;
query?: string;
statsPeriod?: string;
Expand Down
1 change: 0 additions & 1 deletion packages/medusa-plugin-sentry/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export type SentryWebHookOptions = {
export type SentryOptions = Omit<NodeOptions, 'integrations'> & {
integrations: Integration[] | ((router: Router, sentry: typeof Sentry, tracing: typeof Tracing) => Integration[]);
apiToken?: string;
shouldHandleError: (code: number) => boolean;
requestHandlerOptions?: RequestHandlerOptions;
enableRequestHandler?: boolean;
enableTracing?: boolean;
Expand Down

0 comments on commit c37272a

Please sign in to comment.