-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(api-gateway): Fallback to global error middelware for async handlers #7520
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
6 Ignored Deployments
|
); | ||
|
||
/** ************************************************************** | ||
* data scope * | ||
*************************************************************** */ | ||
|
||
app.get(`${this.basePath}/v1/load`, userMiddlewares, (async (req, res) => { | ||
app.get(`${this.basePath}/v1/load`, userMiddlewares, userAsyncHandler(async (req: any, res) => { |
Check failure
Code scanning / CodeQL
Missing rate limiting High
authorization
@@ -254,7 +264,7 @@ | |||
})); | |||
|
|||
const jsonParser = bodyParser.json({ limit: '1mb' }); | |||
app.post(`${this.basePath}/v1/load`, jsonParser, userMiddlewares, (async (req, res) => { | |||
app.post(`${this.basePath}/v1/load`, jsonParser, userMiddlewares, userAsyncHandler(async (req, res) => { |
Check failure
Code scanning / CodeQL
Missing rate limiting High
authorization
@@ -263,7 +273,7 @@ | |||
}); | |||
})); | |||
|
|||
app.get(`${this.basePath}/v1/subscribe`, userMiddlewares, (async (req, res) => { | |||
app.get(`${this.basePath}/v1/subscribe`, userMiddlewares, userAsyncHandler(async (req: any, res) => { |
Check failure
Code scanning / CodeQL
Missing rate limiting High
authorization
@@ -272,31 +282,31 @@ | |||
}); | |||
})); | |||
|
|||
app.get(`${this.basePath}/v1/sql`, userMiddlewares, (async (req, res) => { | |||
app.get(`${this.basePath}/v1/sql`, userMiddlewares, userAsyncHandler(async (req: any, res) => { |
Check failure
Code scanning / CodeQL
Missing rate limiting High
authorization
await this.sql({ | ||
query: req.query.query, | ||
context: req.context, | ||
res: this.resToResultFn(res) | ||
}); | ||
})); | ||
|
||
app.post(`${this.basePath}/v1/sql`, jsonParser, userMiddlewares, (async (req, res) => { | ||
app.post(`${this.basePath}/v1/sql`, jsonParser, userMiddlewares, userAsyncHandler(async (req, res) => { |
Check failure
Code scanning / CodeQL
Missing rate limiting High
authorization
this.resToResultFn(res)({ | ||
timezones: this.scheduledRefreshTimeZones || [] | ||
}); | ||
})); | ||
|
||
app.post('/cubejs-system/v1/pre-aggregations/partitions', jsonParser, systemMiddlewares, (async (req, res) => { | ||
app.post('/cubejs-system/v1/pre-aggregations/partitions', jsonParser, systemMiddlewares, systemAsyncHandler(async (req, res) => { |
Check failure
Code scanning / CodeQL
Missing rate limiting High
authorization
await this.getPreAggregationPartitions({ | ||
query: req.body.query, | ||
context: req.context, | ||
res: this.resToResultFn(res) | ||
}); | ||
})); | ||
|
||
app.post('/cubejs-system/v1/pre-aggregations/preview', jsonParser, systemMiddlewares, (async (req, res) => { | ||
app.post('/cubejs-system/v1/pre-aggregations/preview', jsonParser, systemMiddlewares, systemAsyncHandler(async (req, res) => { |
Check failure
Code scanning / CodeQL
Missing rate limiting High
authorization
await this.getPreAggregationPreview({ | ||
query: req.body.query, | ||
context: req.context, | ||
res: this.resToResultFn(res) | ||
}); | ||
})); | ||
|
||
app.post('/cubejs-system/v1/pre-aggregations/build', jsonParser, systemMiddlewares, (async (req, res) => { | ||
app.post('/cubejs-system/v1/pre-aggregations/build', jsonParser, systemMiddlewares, systemAsyncHandler(async (req, res) => { |
Check failure
Code scanning / CodeQL
Missing rate limiting High
authorization
await this.buildPreAggregations({ | ||
query: req.body.query, | ||
context: req.context, | ||
res: this.resToResultFn(res) | ||
}); | ||
})); | ||
|
||
app.post('/cubejs-system/v1/pre-aggregations/queue', jsonParser, systemMiddlewares, (async (req, res) => { | ||
app.post('/cubejs-system/v1/pre-aggregations/queue', jsonParser, systemMiddlewares, systemAsyncHandler(async (req, res) => { |
Check failure
Code scanning / CodeQL
Missing rate limiting High
authorization
await this.getPreAggregationsInQueue({ | ||
context: req.context, | ||
res: this.resToResultFn(res) | ||
}); | ||
})); | ||
|
||
app.post('/cubejs-system/v1/pre-aggregations/cancel', jsonParser, systemMiddlewares, (async (req, res) => { | ||
app.post('/cubejs-system/v1/pre-aggregations/cancel', jsonParser, systemMiddlewares, systemAsyncHandler(async (req, res) => { |
Check failure
Code scanning / CodeQL
Missing rate limiting High
authorization
* API gateway server class. | ||
*/ | ||
function userAsyncHandler(handler: (req: Request & { context: ExtendedRequestContext }, res: ExpressResponse) => Promise<void>) { | ||
return (req: ExpressRequest, res: ExpressResponse, next: NextFunction) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to add async
before the function to make sure it will not throw a sync error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
handler
is limited with TS by returning type ==> Promise<void>
.- function returns wrapper = sync handler = express.Handler
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can't validate async functions using Typescript:
This snippet does create a function that throws a sync error despite being TS-validated as fully async.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically it's possible. For example, It's possible to do process.exit, but we don't monkey patch process.exit to protect it, because this approach is incorrect and we cannot imagine that someone will write code like this and it will pass review.
} | ||
|
||
function systemAsyncHandler(handler: (req: Request & { context: ExtendedRequestContext }, res: ExpressResponse) => Promise<void>) { | ||
return (req: ExpressRequest, res: ExpressResponse, next: NextFunction) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here as well.
f53655a
to
26fabbc
Compare
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## master #7520 +/- ##
==========================================
+ Coverage 48.01% 48.02% +0.01%
==========================================
Files 154 154
Lines 20897 20903 +6
Branches 5382 5382
==========================================
+ Hits 10033 10039 +6
Misses 10122 10122
Partials 742 742
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Hello!
Thanks