Skip to content

Commit

Permalink
fix error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
hassanelnajjar committed Sep 1, 2023
1 parent 034b965 commit 8e7acd4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
42 changes: 21 additions & 21 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ import GDPRWebhookHandlers from './gdpr.js';
import addSessionShopToReqParams from './middleware/addSessionShopToReqParameters.js';

init();

const errorMiddleware = (err, _req, res, _next) => {
if (err.name === 'ValidationError') {
return res.status(400).json({
success: false,
// @ts-ignore
message: err.message
});
}

// @ts-ignore
return res.status(500).json({
message: err.message,
stack: err.stack
});
};

const STATIC_PATH =
process.env.NODE_ENV === 'production'
? `${process.cwd()}/frontend/dist`
Expand All @@ -27,7 +44,7 @@ app.get(
shopify.redirectToShopifyOrAppRoot()
);

app.use('/fdc', express.json(), fdcRouters);
app.use('/fdc', express.json(), fdcRouters, errorMiddleware);
app.use('/api/*', shopify.validateAuthenticatedSession());

app.use('/*', addSessionShopToReqParams);
Expand All @@ -43,32 +60,15 @@ app.use('/*', shopify.ensureInstalledOnShop(), async (_req, res) =>
.send(readFileSync(join(STATIC_PATH, 'index.html')))
);

app.use(errorMiddleware);

app.post(
shopify.config.webhooks.path,
shopify.processWebhooks({
webhookHandlers: GDPRWebhookHandlers
})
);

app.use((err, _req, res) => {
console.log('errrrrrrrr', err);
console.error(err, {
name: err.name
});

if (err.name === 'ValidationError') {
return res.status(400).json({
success: false,
// @ts-ignore
message: err.message
});
}

// @ts-ignore
return res.status(500).json({
message: err.message,
stack: err.stack
});
});
// add error handler

export default app;
2 changes: 1 addition & 1 deletion web/fdc-modules/orders/controllers/retrieve-order.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ describe('retrieve-order', () => {
]
}
})
.expect(500)
.expect(400)
.expect('Content-Type', /json/)

.expect({
Expand Down

0 comments on commit 8e7acd4

Please sign in to comment.