Source/ClientError.js

"use strict";
/**
 *  @module     ClientError
 *  @overview   Defines the `ClientError` class.
 *
 *  @author     Animesh Mishra <hello@animesh.ltd>
 *  @copyright  © 2018 Animesh Ltd. All Rights Reserved.
 */
Object.defineProperty(exports, "__esModule", { value: true });
/**
 *  The `ClientError` class represents errors whose origin lie client-side. Incorrectly formatted
 *  requests, wrong methods used for API calls and invalid tokens are some examples of a `ClientError`.
 *  An appropriate error information and status code is shared with the client when a `ClientError`
 *  is thrown.
 */
class ClientError extends Error {
    /**
     *  Initialises a `ClientError` with the given HTTP status code, the error message and
     *  an optional debugging help text.
     *
     *  @param { Code } code        HTTP status code for the error response
     *  @param { string } message   The error message
     *  @param { string } help      Optional debugging help text
     *
     *  @constructor
     */
    constructor(code, message, help) {
        super();
        this.code = code;
        this.message = message;
        this.help = help;
    }
    /**
     *  Sanitises a `ClientError` object by getting rid of the `code` property. Sending the
     *  `code` property client-side could make the impression that client shouldn't pay
     *  attention to the HTTP response's `status` header and should code against the response
     *  body's `code` property instead. That would be a bad design choice.
     *
     *  @returns { any } A `ClientError` object without the `code`.
     *
     *  @function Export
     *  @memberof ClientError
     *  @instance
     */
    Export() {
        let publicView = JSON.parse(JSON.stringify(this));
        delete publicView.code;
        return publicView;
    }
}
exports.ClientError = ClientError;
//# sourceMappingURL=ClientError.js.map