From f95a5b4ad93315468cd16e62e69725a317e8ebc0 Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Fri, 1 Oct 2021 05:45:00 -0700 Subject: [PATCH] break(polka): remove `err.code` as `onError` status fallback; - see: https://github.com/sveltejs/kit/issues/2532 --- packages/parse/index.js | 4 ++-- packages/polka/index.js | 4 ++-- packages/polka/test/index.js | 27 ++++----------------------- 3 files changed, 8 insertions(+), 27 deletions(-) diff --git a/packages/parse/index.js b/packages/parse/index.js index d721904..00bba88 100644 --- a/packages/parse/index.js +++ b/packages/parse/index.js @@ -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', () => { @@ -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); diff --git a/packages/polka/index.js b/packages/polka/index.js index dcb0f9e..f613afe 100644 --- a/packages/polka/index.js +++ b/packages/polka/index.js @@ -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]); } @@ -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); } diff --git a/packages/polka/test/index.js b/packages/polka/test/index.js index f985477..19efcfa 100644 --- a/packages/polka/test/index.js +++ b/packages/polka/test/index.js @@ -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) => { @@ -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) => { @@ -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); @@ -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); }