Skip to content

Commit

Permalink
Cleanup for 254593f
Browse files Browse the repository at this point in the history
- Closes hapijs#93
- Closes hapijs#92
  • Loading branch information
arb committed Dec 22, 2015
1 parent f9cfdb3 commit 11b9211
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 9 deletions.
22 changes: 21 additions & 1 deletion README.md
Expand Up @@ -547,6 +547,26 @@ Generates the following response payload:
}
```

### `Boom.illegal([message], [data])`

Returns a 451 Unavailable For Legal Reasons error where:
- `message` - optional message.
- `data` - optional additional error data.

```js
Boom.illegal('you are not permitted to view this resource for legal reasons');
```

Generates the following response payload:

```json
{
"statusCode": 451,
"error": "Unavailable For Legal Reasons",
"message": "you are not permitted to view this resource for legal reasons"
}
```

## HTTP 5xx Errors

All 500 errors hide your message from the end user. Your message is recorded in the server log.
Expand Down Expand Up @@ -656,4 +676,4 @@ Generates the following response payload:

**A** There is a reason the values passed back in the response payloads are pretty locked down. It's mostly for security and to not leak any important information back to the client. This means you will need to put in a little more effort to include extra information about your custom error. Check out the ["Error transformation"](https://github.com/hapijs/hapi/blob/master/API.md#error-transformation) section in the hapi documentation.

---
---
70 changes: 65 additions & 5 deletions lib/index.js
Expand Up @@ -2,13 +2,73 @@

// Load modules

const Http = require('http');
const Hoek = require('hoek');


// Declare internals

const internals = {};
const internals = {
STATUS_CODES: {
__proto__: null,

This comment has been minimized.

Copy link
@AdriVanHoudt

AdriVanHoudt Dec 22, 2015

What does this do?

'100': 'Continue',
'101': 'Switching Protocols',
'102': 'Processing',
'200': 'OK',
'201': 'Created',
'202': 'Accepted',
'203': 'Non-Authoritative Information',
'204': 'No Content',
'205': 'Reset Content',
'206': 'Partial Content',
'207': 'Multi-Status',
'300': 'Multiple Choices',
'301': 'Moved Permanently',
'302': 'Moved Temporarily',
'303': 'See Other',
'304': 'Not Modified',
'305': 'Use Proxy',
'307': 'Temporary Redirect',
'400': 'Bad Request',
'401': 'Unauthorized',
'402': 'Payment Required',
'403': 'Forbidden',
'404': 'Not Found',
'405': 'Method Not Allowed',
'406': 'Not Acceptable',
'407': 'Proxy Authentication Required',
'408': 'Request Time-out',
'409': 'Conflict',
'410': 'Gone',
'411': 'Length Required',
'412': 'Precondition Failed',
'413': 'Request Entity Too Large',
'414': 'Request-URI Too Large',
'415': 'Unsupported Media Type',
'416': 'Requested Range Not Satisfiable',
'417': 'Expectation Failed',
'418': `I'm a teapot`,
'422': 'Unprocessable Entity',
'423': 'Locked',
'424': 'Failed Dependency',
'425': 'Unordered Collection',
'426': 'Upgrade Required',
'428': 'Precondition Required',
'429': 'Too Many Requests',
'431': 'Request Header Fields Too Large',
'451': 'Unavailable For Legal Reasons',
'500': 'Internal Server Error',
'501': 'Not Implemented',
'502': 'Bad Gateway',
'503': 'Service Unavailable',
'504': 'Gateway Time-out',
'505': 'HTTP Version Not Supported',
'506': 'Variant Also Negotiates',
'507': 'Insufficient Storage',
'509': 'Bandwidth Limit Exceeded',
'510': 'Not Extended',
'511': 'Network Authentication Required'
}
};

exports.wrap = function (error, statusCode, message) {

Expand Down Expand Up @@ -71,7 +131,7 @@ internals.initialize = function (error, statusCode, message) {
internals.reformat = function () {

this.output.payload.statusCode = this.output.statusCode;
this.output.payload.error = Http.STATUS_CODES[this.output.statusCode] || 'Unknown';
this.output.payload.error = internals.STATUS_CODES[this.output.statusCode] || 'Unknown';

if (this.output.statusCode === 500) {
this.output.payload.message = 'An internal server error occurred'; // Hide actual error from user
Expand Down Expand Up @@ -268,9 +328,9 @@ exports.tooManyRequests = function (message, data) {
};


exports.censored = function (message, data) {
exports.illegal = function (message, data) {

return internals.create(451, message, data, exports.censored);
return internals.create(451, message, data, exports.illegal);
};


Expand Down
6 changes: 3 additions & 3 deletions test/index.js
Expand Up @@ -495,17 +495,17 @@ describe('tooManyRequests()', () => {
});


describe('censored()', () => {
describe('illegal()', () => {

it('returns a 451 error statusCode', (done) => {

expect(Boom.censored().output.statusCode).to.equal(451);
expect(Boom.illegal().output.statusCode).to.equal(451);
done();
});

it('sets the message with the passed-in message', (done) => {

expect(Boom.censored('my message').message).to.equal('my message');
expect(Boom.illegal('my message').message).to.equal('my message');
done();
});
});
Expand Down

0 comments on commit 11b9211

Please sign in to comment.