Skip to content

Commit

Permalink
break(polka): remove err.code as onError status fallback;
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Oct 1, 2021
1 parent daa2c85 commit f95a5b4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 27 deletions.
4 changes: 2 additions & 2 deletions packages/parse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function parse(opts={}) {
bits += x;
} else {
let err = new Error('Exceeded "Content-Length" limit');
err.code = 413;
err.status = 413;
next(err);
}
}).on('end', () => {
Expand All @@ -39,7 +39,7 @@ export function parse(opts={}) {
req._body = true;
next();
} catch (err) {
err.code = 422;
err.status = 422;
err.details = err.message;
err.message = 'Invalid content';
next(err);
Expand Down
4 changes: 2 additions & 2 deletions packages/polka/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Router from 'trouter';
import { parse } from '@polka/url';

function onError(err, req, res) {
let code = (res.statusCode = err.code || err.status || 500);
let code = (res.statusCode = err.status || 500);
if (typeof err === 'string' || Buffer.isBuffer(err)) res.end(err);
else res.end(err.message || http.STATUS_CODES[code]);
}
Expand All @@ -17,7 +17,7 @@ class Polka extends Router {
this.server = opts.server;
this.handler = this.handler.bind(this);
this.onError = opts.onError || onError; // catch-all handler
this.onNoMatch = opts.onNoMatch || this.onError.bind(null, { code:404 });
this.onNoMatch = opts.onNoMatch || this.onError.bind(null, { status: 404 });
this.attach = (req, res) => setImmediate(this.handler, req, res);
}

Expand Down
27 changes: 4 additions & 23 deletions packages/polka/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ test('errors – `throw Error`', async () => {
polka()
.use(() => {
let err = new Error('hello');
err.code = 418;
err.status = 418;
throw err;
})
.get('/', (req, res) => {
Expand All @@ -964,7 +964,7 @@ test('errors – `throw Error` :: async', async () => {
polka()
.use(async () => {
let err = new Error('hello');
err.code = 418;
err.status = 418;
throw err;
})
.get('/', (req, res) => {
Expand Down Expand Up @@ -1268,25 +1268,6 @@ test('options.onError', async () => {
});


test('options.onError (err.code)', async () => {
// t.plan(3);

let foo = new Error('Oops!');
foo.code = 418;

let app = polka().use((req, res, next) => next(foo));
$.isFunction(app.onError, '~> attaches default `app.onError` handler');

let uri = $.listen(app);
await get(uri).catch(err => {
assert.is(err.statusCode, 418, '~> response has 418 code (via "err.code" key)');
assert.is(err.data, 'Oops!', '~> response body is "Oops!" string');
});

app.server.close();
});


test('options.onError (err.status)', async () => {
// t.plan(3);

Expand All @@ -1310,14 +1291,14 @@ test('options.onError – custom', async () => {
// t.plan(7);

let foo = new Error('boo~!');
foo.code = 418;
foo.status = 418;

function onError(err, req, res, next) {
assert.equal(err, foo, '~> receives the `err` object directly as 1st param');
assert.ok(req.url, '~> receives the `req` object as 2nd param');
$.isFunction(res.end, '~> receives the `res` object as 3rd param');
$.isFunction(next, '~> receives the `next` function 4th param'); // in case want to skip?
res.statusCode = err.code;
res.statusCode = err.status;
res.end('error: ' + err.message);
}

Expand Down

0 comments on commit f95a5b4

Please sign in to comment.