mirror of
https://github.com/abrendan/MicDropMessages.git
synced 2025-08-25 14:02:03 +02:00
Initial commit
This commit is contained in:
51
node_modules/server/error/README.md
generated
vendored
Normal file
51
node_modules/server/error/README.md
generated
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
# Experimental syntax proposal
|
||||
|
||||
```js
|
||||
// Using a factory
|
||||
const ErrorFactory = require('server/error');
|
||||
const ServerError = ErrorFactory('/server/', {});
|
||||
|
||||
const ErrorFactory = require('server/error');
|
||||
class ServerError extends ErrorFactory {
|
||||
namespace: '/server',
|
||||
url: ({ id }) => ...,
|
||||
status: 500
|
||||
}
|
||||
|
||||
// Using the plain import method
|
||||
const ServerError = require('server/error')('/server/');
|
||||
const ServerError = require('server/error')('/server/', {
|
||||
namespace: '/server/',
|
||||
url: ({ slug }) => `https://serverjs.io/documentation/errors/#${slug}`,
|
||||
status: 500,
|
||||
});
|
||||
const ServerError = require('server/error')({
|
||||
namespace: '/server/'
|
||||
});
|
||||
|
||||
const SassError = require('server/error')({
|
||||
namespace: '/plugin/sass',
|
||||
url: ({ slug }) => `https://serverjs.io/documentation/errors/#${slug}`,
|
||||
});
|
||||
|
||||
const ServerError = require('server/error');
|
||||
|
||||
const SassError = ServerError(null, {
|
||||
namespace: '/plugin/sass',
|
||||
status: 500
|
||||
});
|
||||
|
||||
const SassError = ServerError.defaults({
|
||||
namespace: '/plugin/sass/',
|
||||
status: 500
|
||||
});
|
||||
|
||||
SassError.exists = ({ file }) => `
|
||||
The file "${file}" already exists. Please rename it or remove it so @sass/server
|
||||
can work properly. <3
|
||||
`;
|
||||
|
||||
throw new SassError('exists');
|
||||
throw new SassError('exists', { status: 500 });
|
||||
throw new SassError('exists', { file: FILENAME });
|
||||
```
|
25
node_modules/server/error/index.js
generated
vendored
Normal file
25
node_modules/server/error/index.js
generated
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
const buildError = (message, opts) => {
|
||||
const error = new Error(message);
|
||||
for (const key in opts) {
|
||||
error[key] = opts[key] instanceof Function ? opts[key](opts) : opts[key];
|
||||
}
|
||||
return error;
|
||||
};
|
||||
|
||||
const singleSlash = str => '/' + str.split('/').filter(one => one).join('/');
|
||||
|
||||
const ErrorFactory = function (namespace = '', defaults = {}) {
|
||||
defaults.namespace = defaults.namespace || namespace;
|
||||
|
||||
return function ErrorInstance (code = '', options = {}) {
|
||||
options = Object.assign({}, ErrorFactory.options, defaults, options);
|
||||
options.code = singleSlash(options.namespace + '/' + code);
|
||||
options.id = options.code.toLowerCase().replace(/[^\w]+/g, '-').replace(/^-/, '');
|
||||
options.message = ErrorInstance[code];
|
||||
return buildError(options.message, options);
|
||||
};
|
||||
};
|
||||
|
||||
ErrorFactory.options = { status: 500 };
|
||||
|
||||
module.exports = ErrorFactory;
|
85
node_modules/server/error/index.test.js
generated
vendored
Normal file
85
node_modules/server/error/index.test.js
generated
vendored
Normal file
@@ -0,0 +1,85 @@
|
||||
const ErrorFactory = require('./index.js');
|
||||
|
||||
describe('server/error', () => {
|
||||
it('is a function', () => {
|
||||
expect(ErrorFactory instanceof Function).toBe(true);
|
||||
});
|
||||
|
||||
it('canNOT do simple errors', () => {
|
||||
expect(new ErrorFactory('Hello world').message).not.toBe('Hello world');
|
||||
expect(new ErrorFactory('Hello world') instanceof Error).not.toBe(true);
|
||||
});
|
||||
|
||||
it('does not create a plain error', () => {
|
||||
expect(ErrorFactory().message).toBe(undefined);
|
||||
expect(ErrorFactory() instanceof Function).toBe(true);
|
||||
expect(ErrorFactory('Hello world').message).toBe(undefined);
|
||||
expect(ErrorFactory('Hello world') instanceof Function).toBe(true);
|
||||
expect(ErrorFactory('Hello world', {}).message).toBe(undefined);
|
||||
expect(ErrorFactory('Hello world', {}) instanceof Function).toBe(true);
|
||||
});
|
||||
|
||||
it('can create errors from within the factory', () => {
|
||||
expect(ErrorFactory('/server/')('test') instanceof Error).toBe(true);
|
||||
expect(ErrorFactory('/server/')('test').code).toBe('/server/test');
|
||||
expect(ErrorFactory('/server/')('test', { status: 500 }).status).toBe(500);
|
||||
});
|
||||
|
||||
it('can create errors from within the factory', () => {
|
||||
expect(ErrorFactory('/server/')('test') instanceof Error).toBe(true);
|
||||
expect(ErrorFactory('/server/')('test').code).toBe('/server/test');
|
||||
expect(ErrorFactory('/server/')('test').namespace).toBe('/server/');
|
||||
expect(ErrorFactory('/server/')('test', { status: 500 }).status).toBe(500);
|
||||
});
|
||||
|
||||
describe('Namespaces', () => {
|
||||
const TestError = ErrorFactory('/server/', { status: 500 });
|
||||
|
||||
it('has the correct defaults', () => {
|
||||
expect(TestError().status).toBe(500);
|
||||
expect(TestError().code).toBe('/server');
|
||||
expect(TestError().id).toBe('server');
|
||||
});
|
||||
|
||||
it('can extend the errors', () => {
|
||||
const err = TestError('demo', { status: 501 });
|
||||
expect(err.status).toBe(501);
|
||||
expect(err.code).toBe('/server/demo');
|
||||
expect(err.id).toBe('server-demo');
|
||||
});
|
||||
|
||||
it('is the same as with the instance', () => {
|
||||
const err = TestError('demo', { status: 501 });
|
||||
const err2 = new TestError('demo', { status: 501 });
|
||||
expect(err).toMatchObject(err2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Define errors', () => {
|
||||
const TestError = ErrorFactory('/server/', { status: 500 });
|
||||
TestError.aaa = 'First error';
|
||||
|
||||
it('has the correct message', () => {
|
||||
expect(TestError('aaa').message).toBe('First error');
|
||||
});
|
||||
|
||||
it('can define an error with a function', () => {
|
||||
TestError.bbb = () => `Function error`;
|
||||
expect(TestError('bbb').message).toBe('Function error');
|
||||
});
|
||||
|
||||
it('defines errors globally', () => {
|
||||
expect(TestError('bbb').message).toBe('Function error');
|
||||
});
|
||||
|
||||
it('errors are namespaced', () => {
|
||||
const TestError = ErrorFactory('/server/');
|
||||
expect(TestError('bbb').message).toBe(undefined);
|
||||
});
|
||||
|
||||
it('gets the options in the interpolation', () => {
|
||||
TestError.ccc = ({ status }) => `Function error ${status}`;
|
||||
expect(TestError('ccc', { status: 505 }).message).toBe('Function error 505');
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user