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

Commits on May 6, 2019

  1. Allow custom errors to specify response HTTP codes

    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.
    crrobinson14 committed May 6, 2019
    Configuration menu
    Copy the full SHA
    fc8a77b View commit details
    Browse the repository at this point in the history
  2. Comply with standard

    crrobinson14 committed May 6, 2019
    Configuration menu
    Copy the full SHA
    e98d25c View commit details
    Browse the repository at this point in the history

Commits on May 27, 2019

  1. Configuration menu
    Copy the full SHA
    8075b9e View commit details
    Browse the repository at this point in the history

Commits on Jun 12, 2019

  1. Configuration menu
    Copy the full SHA
    f006bc8 View commit details
    Browse the repository at this point in the history