return (request, __, next) => { if (request.method === 'GET' || request.method === 'HEAD') { next(createError(404)); } else { next(createError(501)); } };
app.use((req, res, next) => { if (!req) { next(create('Please login to view this page.', 401)); return; } next(); });
export const compareBHash = async (value: string, hash: string): Promise<true> => { const isValid = await bcrypt.compare(value, hash); if (!isValid) { throw createError(401, 'No match found'); } return isValid; };
router.get("/", (req, res, next) => { const name = req.query.name as string; if (name) { res.send({ greetingMessage: greet(name) }); } else { const err = HttpErrors(400, "Bad Request"); next(err); } });
registerSuite('lib/middleware/finalError', function() { let handler: (error: any, request: any, response: any, next: any) => void; let request: MockRequest; let response: MockResponse; let next: SinonSpy; return { beforeEach() { handler = finalError(); request = new MockRequest('GET', '/foo/bar.js'); response = new MockResponse(); next = spy(); }, tests: { 'exposed message'() { const error = createError(500, 'b0rked', { expose: true }); handler(error, request, response, next); assert.isFalse(next.called); assert.match(response.data, /500 b0rked/); assert.strictEqual(response.statusCode, 500); }, 'hidden message'() { const error = createError(404, 'b0rked', { expose: false }); handler(error, request, response, next); assert.isFalse(next.called); assert.match(response.data, /404 Not Found/); assert.strictEqual(response.statusCode, 404); } } }; });
readFile(wholePath, 'utf8', (error, data) => { // The server was stopped in the middle of the file read if (context.stopped) { return; } if (error) { return next(createError(404, error, { expose: false })); } // providing `wholePath` to the instrumenter instead of a // partial filename is necessary because lcov.info requires // full path names as per the lcov spec data = executor.instrumentCode(data, wholePath); codeCache[wholePath] = { // strictly speaking mtime could reflect a previous // version, assume those race conditions are rare mtime, data }; send(contentType, data); });
import * as util from 'util'; const app = express(); app.use((req, res, next) => { if (!req) { next(create('Please login to view this page.', 401)); return; } next(); }); /* Examples taken from https://github.com/jshttp/http-errors/blob/1.6.2/test/test.js */ // create(status) let err = create(404); err; // $ExpectType HttpError err.name; // $ExpectType string err.message; // $ExpectType string err.status; // $ExpectType number err.statusCode; // $ExpectType number err.expose; // $ExpectType boolean err.headers; // $ExpectType { [key: string]: string; } | undefined // create(status, msg) err = create(404, 'LOL'); // create(status, props) err = create(404, {id: 1}); // create(status, props) with status prop
stat(wholePath, (error, stats) => { // The server was stopped before this file was served if (context.stopped) { return; } if (error || !stats.isFile()) { executor.log('Unable to serve', wholePath, '(unreadable)'); return next(createError(404, error, { expose: false })); } executor.log('Serving', wholePath); const send = (contentType: string, data: string) => { response.writeHead(200, { 'Content-Type': contentType, 'Content-Length': Buffer.byteLength(data) }); response.end(request.method === 'HEAD' ? '' : data, callback); }; const callback = (error?: Error) => { if (error) { executor.emit( 'error', new Error(`Error serving ${wholePath}: ${error.message}`) ); } else { executor.log('Served', wholePath); } }; const contentType = lookup(wholePath) || 'application/octet-stream'; const mtime = stats.mtime.getTime(); if (codeCache[wholePath] && codeCache[wholePath].mtime === mtime) { send(contentType, codeCache[wholePath].data); } else { readFile(wholePath, 'utf8', (error, data) => { // The server was stopped in the middle of the file read if (context.stopped) { return; } if (error) { return next(createError(404, error, { expose: false })); } // providing `wholePath` to the instrumenter instead of a // partial filename is necessary because lcov.info requires // full path names as per the lcov spec data = executor.instrumentCode(data, wholePath); codeCache[wholePath] = { // strictly speaking mtime could reflect a previous // version, assume those race conditions are rare mtime, data }; send(contentType, data); }); } });
app.use(function(req: Request, res: Response, next: NextFunction) { let err: HttpError = Error("Not Found"); err.status = 404; next(err); });