Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom errors to specify response HTTP codes #1301

Closed
wants to merge 4 commits into from

Conversation

crrobinson14
Copy link
Collaborator

Currently if you throw an error during any action, the response code is always 400. This change allows you to use the following pattern:

function NotFoundError(message, extraFields) {
    Error.call(this);

    this.name = this.constructor.name;
    this.message = message || '404: Not Found';
    this.responseHttpCode = 404;
    Object.assign(this, extraFields || {});
}

then in an action:

const { NotFoundError } = require('../../lib/customErrors');
...
const user = api.models.User.findByPk(userId);
if (!user) {
    throw new NotFoundError('Invalid userId');
}

This will result in a "Invalid userId" message passed back, with a responseHttpCode of 404 (not found). You can define as many error types as you want this way, e.g. using SessionError/AuthError for indicating 401/403 responses, ServerError for 5xx series errors (remote database failed), etc.

Currently if you throw an error during any action, the response code is always 400. This change allows you to use the following pattern:

```
function NotFoundError(message, extraFields) {
    Error.call(this);

    this.name = this.constructor.name;
    this.message = message || '404: Not Found';
    this.responseHttpCode = 404;
    Object.assign(this, extraFields || {});
}
```

then in an action:

```
const { NotFoundError } = require('../../lib/customErrors');
...
const user = api.models.User.findByPk(userId);
if (!user) {
    throw new NotFoundError('Invalid userId');
}
```

This will result in a "Invalid userId" message passed back, with a responseHttpCode of 404 (not found). You can define as many error types as you want this way, e.g. using `SessionError/AuthError` for indicating 401/403 responses, `ServerError` for 5xx series errors (remote database failed), etc.
@evantahler
Copy link
Member

Love the idea! Can you please add some tests for the new behavior?

@evantahler
Copy link
Member

@crrobinson14 ping! Any update?

@evantahler
Copy link
Member

... still waiting on some tests @crrobinson14 ...

@evantahler
Copy link
Member

@piyushagwl007 This might be a great place to start after the tuotiral

@evantahler
Copy link
Member

closing in favor of #1334

@evantahler evantahler closed this Oct 7, 2019
@evantahler evantahler deleted the crrobinson14/error-response-codes branch October 7, 2019 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants