-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure all API endpoints are safe against uncaught promise rejections
- Loading branch information
Showing
7 changed files
with
179 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
import type { RequestHandler } from 'express'; | ||
|
||
export function safe<H extends RequestHandler>(handler: H): H { | ||
return (async (req, res, next) => { | ||
// express 4.x does not handle asynchronous errors - if an asynchronous operation fails, it will crash the server | ||
// we defend against this by wrapping all async handlers in this function, which mimics the behaviour of express 5.x | ||
export function safe<P = unknown>( | ||
handler: RequestHandler<P>, | ||
): RequestHandler<P> { | ||
return async (req, res, next) => { | ||
try { | ||
await handler(req, res, next); | ||
} catch (e) { | ||
next(e); | ||
} | ||
}) as H; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,36 @@ | ||
import { Router } from 'websocket-express'; | ||
import { type GiphyService } from '../services/GiphyService'; | ||
import { logError } from '../log'; | ||
import { safe } from '../helpers/routeHelpers'; | ||
|
||
export class ApiGiphyRouter extends Router { | ||
public constructor(service: GiphyService) { | ||
super(); | ||
|
||
this.get('/search', async (req, res) => { | ||
const { q, lang = 'en' } = req.query; | ||
this.get( | ||
'/search', | ||
safe(async (req, res) => { | ||
const { q, lang = 'en' } = req.query; | ||
|
||
if (typeof q !== 'string' || !q) { | ||
res.status(400).json({ error: 'Bad request' }); | ||
return; | ||
} | ||
if (typeof q !== 'string' || !q) { | ||
res.status(400).json({ error: 'Bad request' }); | ||
return; | ||
} | ||
|
||
if (typeof lang !== 'string') { | ||
res.status(400).json({ error: 'Bad request' }); | ||
return; | ||
} | ||
if (typeof lang !== 'string') { | ||
res.status(400).json({ error: 'Bad request' }); | ||
return; | ||
} | ||
|
||
try { | ||
const gifs = await service.search(q, 10, lang); | ||
try { | ||
const gifs = await service.search(q, 10, lang); | ||
|
||
res.json({ gifs }); | ||
} catch (err) { | ||
logError('Giphy proxy error', err); | ||
res.status(500).json({ error: 'Proxy error' }); | ||
} | ||
}); | ||
res.json({ gifs }); | ||
} catch (err) { | ||
logError('Giphy proxy error', err); | ||
res.status(500).json({ error: 'Proxy error' }); | ||
} | ||
}), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.