Initial commit

This commit is contained in:
abrendan
2023-11-30 14:15:19 +00:00
commit e4599df811
5457 changed files with 500139 additions and 0 deletions

40
node_modules/server/plugins/final/errors.js generated vendored Normal file
View File

@@ -0,0 +1,40 @@
const error = require('../../error')('/plugin/final/');
error.noreturn = ({ method, url }) => `
Your middleware did not return anything for this request:
${method} ${url}
This normally happens when no route was matched or if the router did not reply with anything. Make sure to return something, even if it's a catch-all error.
Documentation for reply: https://serverjs.io/documentation/reply/
Relevant issue: https://github.com/franciscop/server/issues/118
`;
error.unhandled = `
Some middleware threw an error that was not handled properly. This can happen when you do this:
~~~
// BAD:
server(ctx => { throw new Error('I am an error!'); });
~~~
To catch and handle these types of errors, add a route to the end of your middlewares to handle errors like this:
~~~
// GOOD:
const { error } = server.router;
const { status } = server.reply;
server(
ctx => { throw new Error('I am an error!'); },
// ...
error(ctx => status(500).send(ctx.error.message))
);
~~~
Please feel free to open an issue in Github asking for more info:
https://github.com/franciscop/server
`;
module.exports = error;

68
node_modules/server/plugins/final/final.test.js generated vendored Normal file
View File

@@ -0,0 +1,68 @@
const run = require('server/test/run');
// Note: the `raw` option only works for tests
const storeLog = out => ({ report: { write: log => { out.log = log; } } });
describe('final', () => {
it('gets called with an unhandled error', async () => {
const simple = () => { throw new Error('Hello Error'); };
const out = {};
const res = await run({ raw: true, log: storeLog(out) }, simple).get('/');
expect(res.statusCode).toBe(500);
expect(res.body).toBe('Internal Server Error');
expect(out.log).toMatch('Hello Error');
});
it('just logs it if the headers were already sent', async () => {
const simple = () => { throw new Error('Hello Error'); };
const out = {};
const res = await run({ raw: true, log: storeLog(out) }, () => 'Hello world', simple).get('/');
expect(res.statusCode).toBe(200);
expect(res.body).toBe('Hello world');
expect(out.log).toMatch('Hello Error');
});
it('displays the appropriate error to the public', async () => {
const simple = () => {
const err = new Error('Hello Error: display to the public');
err.public = true;
throw err;
};
const out = {};
const res = await run({ raw: true, log: storeLog(out) }, simple).get('/');
expect(res.statusCode).toBe(500);
expect(res.body).toBe('Hello Error: display to the public');
expect(out.log).toMatch('Hello Error');
});
it('makes the status 500 if it is invalid', async () => {
const simple = () => {
const err = new Error('Hello Error');
err.status = 'pepito';
throw err;
};
const out = {};
const res = await run({ raw: true, log: storeLog(out) }, simple).get('/');
expect(res.statusCode).toBe(500);
expect(res.body).toBe('Internal Server Error');
expect(out.log).toMatch('Hello Error');
});
it('does not reply if the headers are already sent', async () => {
const simple = ctx => {
ctx.res.send('Error 世界');
throw new Error('Hello');
};
const res = await run(simple).get('/');
expect(res.body).toBe('Error 世界');
});
it('handles non-existing requests to a 404', async () => {
const out = {};
const res = await run({ log: storeLog(out) }).get('/non-existing');
expect(res.statusCode).toBe(404);
expect(out.log).toMatch(/did not return anything/);
});
});

44
node_modules/server/plugins/final/index.js generated vendored Normal file
View File

@@ -0,0 +1,44 @@
// This file makes sure to clean up things in case there was something missing
// There are two reasons normally for this to happen: no reply was set or an
// unhandled error was thrown
const FinalError = require('./errors');
// Make sure that a (404) reply is sent if there was no user reply
const handler = async ctx => {
if (!ctx.res.headersSent) {
// Send the user-set status
ctx.res.status(ctx.res.explicitStatus ? ctx.res.statusCode : 404).send();
// Show it only if there was no status set in a return
if (!ctx.res.explicitStatus) {
ctx.log.error(
new FinalError('noreturn', { url: ctx.url, method: ctx.method })
);
}
}
};
// Make sure there is a (500) reply if there was an unhandled error thrown
handler.error = ctx => {
const error = ctx.error;
ctx.log.warning(FinalError('unhandled'));
ctx.log.error(error);
if (!ctx.res.headersSent) {
let status = error.status || error.code || 500;
if (typeof status !== 'number') status = 500;
// Display the error message if this error is marked as public
if (error.public) {
return ctx.res.status(status).send(error.message);
}
// Otherwise just display the default error for that code
ctx.res.sendStatus(status);
}
};
module.exports = {
name: 'final',
after: handler
};
// module.exports = handler;