UNPKG

@mdf.js/crash

Version:

MMS - API Crash - Enhanced error management library

524 lines 29.5 kB
"use strict"; /** * Copyright 2024 Mytra Control S.L. All rights reserved. * * Use of this source code is governed by an MIT-style license that can be found in the LICENSE file * or at https://opensource.org/licenses/MIT. aaa */ Object.defineProperty(exports, "__esModule", { value: true }); exports.BoomHelpers = void 0; const HTTPCode_t_1 = require("../types/HTTPCode.t"); const BoomError_1 = require("./BoomError"); /** * Helpers for easy generation of Boom kind errors * - Client error (`400`-`499`) * - {@link badRequest | `400 Bad Request`} * - {@link unauthorized | `401 Unauthorized`} * - {@link paymentRequired | `402 Payment Required`} * - {@link forbidden | `403 Forbidden`} * - {@link notFound | `404 Not Found`} * - {@link methodNotAllowed | `405 Method Not Allowed`} * - {@link notAcceptable | `406 Not Acceptable`} * - {@link proxyAuthRequired | `407 Proxy Authentication Required`} * - {@link requestTimeout | `408 Request Timeout`} * - {@link conflict | `409 Conflict`} * - {@link gone | `410 Gone`} * - {@link lengthRequired | `411 Length Required`} * - {@link preconditionFailed | `412 Precondition Failed`} * - {@link payloadTooLarge | `413 Payload Too Large`} * - {@link uriTooLong | `414 URI Too Long`} * - {@link unsupportedMediaType | `415 Unsupported Media Type`} * - {@link rangeNotSatisfiable | `416 Range Not Satisfiable`} * - {@link expectationFailed | `417 Expectation Failed`} * - {@link teapot | `418 I'm a teapot`} * - {@link unprocessableEntity | `422 Unprocessable Entity`} * - {@link locked | `423 Locked`} * - {@link failedDependency | `424 Failed Dependency`} * - {@link tooEarly | `425 Too Early`} * - {@link upgradeRequired | `426 Upgrade Required`} * - {@link preconditionRequired | `428 Precondition Required`} * - {@link tooManyRequests | `429 Too Many Requests`} * - {@link headerFieldsTooLarge | `431 Request Header Fields Too Large`} * - {@link illegal | `451 Unavailable For Legal Reasons`} * - Server error (`500`-`599`) * - {@link internalServerError | `500 Internal Server Error`} * - {@link notImplemented | `501 Not Implemented`} * - {@link badGateway | `502 Bad Gateway`} * - {@link serverUnavailable | `503 Service Unavailable`} * - {@link gatewayTimeout | `504 Gateway Timeout`} * @category Boom * @public */ class BoomHelpers { /** Private constructor */ constructor() { } } exports.BoomHelpers = BoomHelpers; /** The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the * server cannot or will not process the request due to something that is perceived to be a client * error (e.g., malformed request syntax, invalid request message framing, or deceptive request * routing). * The client should not repeat this request without modification. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.badRequest = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.BAD_REQUEST, options); }; /** * The HTTP 401 Unauthorized client error status response code indicates that the request has not * been applied because it lacks valid authentication credentials for the target resource. * This status is sent with a WWW-Authenticate header that contains information on how to * authorize correctly. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.unauthorized = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.UNAUTHORIZED, options); }; /** * The HTTP 402 Payment Required is a nonstandard client error status response code that is * reserved for future use. * Sometimes, this code indicates that the request can not be processed until the client makes a * payment. Originally it was created to enable digital cash or (micro) payment systems and would * indicate that the requested content is not available until the client makes a payment. However, * no standard use convention exists and different entities use it in different contexts. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.paymentRequired = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.PAYMENT_REQUIRED, options); }; /** * The HTTP 403 Forbidden client error status response code indicates that the server understood * the request but refuses to authorize it. * This status is similar to 401, but in this case, re-authenticating will make no difference. The * access is permanently forbidden and tied to the application logic, such as insufficient rights * to a resource. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.forbidden = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.FORBIDDEN, options); }; /** * The HTTP 404 Not Found client error response code indicates that the server can't find the * requested resource. Links which lead to a 404 page are often called broken or dead links, and * can be subject to link rot. * A 404 status code does not indicate whether the resource is temporarily or permanently missing. * But if a resource is permanently removed, a 410 (Gone) should be used instead of a 404 status. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.notFound = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.NOT_FOUND, options); }; /** * The HyperText Transfer Protocol (HTTP) 405 Method Not Allowed response status code indicates * that the request method is known by the server but is not supported by the target resource. The * server MUST generate an Allow header field in a 405 response containing a list of the target * resource's currently supported methods. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.methodNotAllowed = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.METHOD_NOT_ALLOWED, options); }; /** * The HyperText Transfer Protocol (HTTP) 406 Not Acceptable client error response code indicates * that the server cannot produce a response matching the list of acceptable values defined in the * request's proactive content negotiation headers, and that the server is unwilling to supply a * default representation. * In practice, this error is very rarely used. Instead of responding using this error code, which * would be cryptic for the end user and difficult to fix, servers ignore the relevant header and * serve an actual page to the user. It is assumed that even if the user won't be completely * happy, they will prefer this to an error code. * If a server returns such an error status, the body of the message should contain the list of * the available representations of the resources, allowing the user to choose among them. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.notAcceptable = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.NOT_ACCEPTABLE, options); }; /** * The HTTP 407 Proxy Authentication Required client error status response code indicates that the * request has not been applied because it lacks valid authentication credentials for a proxy * server that is between the browser and the server that can access the requested resource. * This status is sent with a Proxy-Authenticate header that contains information on how to * authorize correctly. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.proxyAuthRequired = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.PROXY_AUTHENTICATION_REQUIRED, options); }; /** * The HyperText Transfer Protocol (HTTP) 408 Request Timeout response status code means that the * server would like to shut down this unused connection. It is sent on an idle connection by some * servers, even without any previous request by the client. * A server should send the "close" Connection header field in the response, since 408 implies * that the server has decided to close the connection rather than continue waiting. * This response is used much more since some browsers, like Chrome, Firefox 27+, and IE9, use * HTTP pre-connection mechanisms to speed up surfing. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.requestTimeout = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.REQUEST_TIMEOUT, options); }; /** * The HTTP 409 Conflict response status code indicates a request conflict with current state of * the server. * Conflicts are most likely to occur in response to a PUT request. For example, you may get a 409 * response when uploading a file which is older than the one already on the server resulting in a * version control conflict. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.conflict = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.CONFLICT, options); }; /** * The HyperText Transfer Protocol (HTTP) 410 Gone client error response code indicates that * access to the target resource is no longer available at the origin server and that this * condition is likely to be permanent. * If you don't know whether this condition is temporary or permanent, a 404 status code should be * used instead. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.gone = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.GONE, options); }; /** * The HyperText Transfer Protocol (HTTP) 411 Length Required client error response code indicates * that the server refuses to accept the request without a defined Content-Length header. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.lengthRequired = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.LENGTH_REQUIRED, options); }; /** * The HyperText Transfer Protocol (HTTP) 412 Precondition Failed client error response code * indicates that access to the target resource has been denied. This happens with conditional * requests on methods other than GET or HEAD when the condition defined by the * If-Unmodified-Since or If-None-Match headers is not fulfilled. In that case, the request, * usually an upload or a modification of a resource, cannot be made and this error response is * sent back. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.preconditionFailed = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.PRECONDITION_FAILED, options); }; /** * The HTTP 413 Payload Too Large response status code indicates that the request entity is larger * than limits defined by server; the server might close the connection or return a Retry-After * header field. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.payloadTooLarge = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.PAYLOAD_TOO_LARGE, options); }; /** * The HTTP 414 URI Too Long response status code indicates that the URI requested by the client * is longer than the server is willing to interpret. * There are a few rare conditions when this might occur: * - when a client has improperly converted a POST request to a GET request with long query * information, * - when the client has descended into a loop of redirection (for example, a redirected URI * prefix that points to a suffix of itself), * - or when the server is under attack by a client attempting to exploit potential security * holes * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.uriTooLong = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.URI_TOO_LONG, options); }; /** * The HTTP 415 Unsupported Media Type client error response code indicates that the server * refuses to accept the request because the payload format is in an unsupported format. * The format problem might be due to the request's indicated Content-Type or Content-Encoding, or * as a result of inspecting the data directly. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.unsupportedMediaType = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.UNSUPPORTED_MEDIA_TYPE, options); }; /** * The HyperText Transfer Protocol (HTTP) 416 Range Not Satisfiable error response code indicates * that a server cannot serve the requested ranges. The most likely reason is that the document * doesn't contain such ranges, or that the Range header value, though syntactically correct, * doesn't make sense. * The 416 response message contains a Content-Range indicating an unsatisfied range (that is a * '*') followed by a '/' and the current length of the resource. E.g. Content-Range: bytes /12777 * Faced with this error, browsers usually either abort the operation (for example, a download * will be considered as non-resumable) or ask for the whole document again. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.rangeNotSatisfiable = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.RANGE_NOT_SATISFIABLE, options); }; /** * The HTTP 417 Expectation Failed client error response code indicates that the expectation given * in the request's Expect header could not be met. * See the Expect header for more details. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.expectationFailed = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.EXPECTATION_FAILED, options); }; /** * The HTTP 418 I'm a teapot client error response code indicates that the server refuses to brew * coffee because it is, permanently, a teapot. A combined coffee/tea pot that is temporarily out * of coffee should instead return 503. This error is a reference to Hyper Text Coffee Pot Control * Protocol defined in April Fools' jokes in 1998 and 2014. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.teapot = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.I_AM_A_TEAPOT, options); }; /** * The HyperText Transfer Protocol (HTTP) 422 Unprocessable Entity response status code indicates * that the server understands the content type of the request entity, and the syntax of the * request entity is correct, but it was unable to process the contained instructions. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.unprocessableEntity = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.UNPROCESSABLE_ENTITY, options); }; /** * The 423 (Locked) status code means the source or destination resource of a method is locked. * This response SHOULD contain an appropriate precondition or postcondition code, such as * 'lock-token-submitted' or 'no-conflicting-lock'. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.locked = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.LOCKED, options); }; /** * The 424 (Failed Dependency) status code means that the method could not be performed on the * resource because the requested action depended on another action and that action failed. For * example, if a command in a PROPPATCH method fails, then, at minimum, the rest of the commands * will also fail with 424 (Failed Dependency). * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.failedDependency = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.FAILED_DEPENDENCY, options); }; /** * The HyperText Transfer Protocol (HTTP) 425 Too Early response status code indicates that the * server is unwilling to risk processing a request that might be replayed, which creates the * potential for a replay attack. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.tooEarly = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.TOO_EARLY, options); }; /** * The HTTP 426 Upgrade Required client error response code indicates that the server refuses to * perform the request using the current protocol but might be willing to do so after the client * upgrades to a different protocol. * The server sends an Upgrade header with this response to indicate the required protocol(s). * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.upgradeRequired = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.UPGRADE_REQUIRED, options); }; /** * The HTTP 428 Precondition Required response status code indicates that the server requires the * request to be conditional. * Typically, this means that a required precondition header, such as If-Match, is missing. * When a precondition header is not matching the server side state, the response should be 412 * Precondition Failed. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.preconditionRequired = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.PRECONDITION_REQUIRED, options); }; /** * The HTTP 429 Too Many Requests response status code indicates the user has sent too many * requests in a given amount of time ("rate limiting"). * A Retry-After header might be included to this response indicating how long to wait before * making a new request * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.tooManyRequests = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.TOO_MANY_REQUESTS, options); }; /** * The HTTP 431 Request Header Fields Too Large response status code indicates that the server * refuses to process the request because the request’s HTTP headers are too long. The request may * be resubmitted after reducing the size of the request headers. * 431 can be used when the total size of request headers is too large, or when a single header * field is too large. To help those running into this error, indicate which of the two is the * problem in the response body — ideally, also include which headers are too large. This lets * users attempt to fix the problem, such as by clearing their cookies. * Servers will often produce this status if: * - The Referer URL is too long * - There are too many Cookies sent in the request * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.headerFieldsTooLarge = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.REQUEST_HEADER_FIELDS_TOO_LARGE, options); }; /** * The HyperText Transfer Protocol (HTTP) 451 Unavailable For Legal Reasons client error response * code indicates that the user requested a resource that is not available due to legal reasons, * such as a web page for which a legal action has been issued. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.illegal = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.UNAVAILABLE_FOR_LEGAL_REASONS, options); }; /** * The HyperText Transfer Protocol (HTTP) 500 Internal Server Error server error response code * indicates that the server encountered an unexpected condition that prevented it from fulfilling * the request. * This error response is a generic "catch-all" response. Usually, this indicates the server * cannot find a better 5xx error code to response. Sometimes, server administrators log error * responses like the 500 status code with more details about the request to prevent the error * from happening again in the future. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.internalServerError = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.INTERNAL_SERVER_ERROR, options); }; /** * The HyperText Transfer Protocol (HTTP) 501 Not Implemented server error response code means * that the server does not support the functionality required to fulfill the request. * This status can also send a Retry-After header, telling the requester when to check back to see * if the functionality is supported by then. * 501 is the appropriate response when the server does not recognize the request method and is * incapable of supporting it for any resource. The only methods that servers are required to * support (and therefore that must not return 501) are GET and HEAD. * If the server does recognize the method, but intentionally does not support it, the appropriate * response is 405 Method Not Allowed. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.notImplemented = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.NOT_IMPLEMENTED, options); }; /** * The HyperText Transfer Protocol (HTTP) 502 Bad Gateway server error response code indicates * that the server, while acting as a gateway or proxy, received an invalid response from the * upstream server. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.badGateway = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.BAD_GATEWAY, options); }; /** * The HyperText Transfer Protocol (HTTP) 503 Service Unavailable server error response code * indicates that the server is not ready to handle the request. * Common causes are a server that is down for maintenance or that is overloaded. This response * should be used for temporary conditions and the Retry-After HTTP header should, if possible, * contain the estimated time for the recovery of the service. * Caching-related headers that are sent along with this response should be taken care of, as a * 503 status is often a temporary condition and responses shouldn't usually be cached. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.serverUnavailable = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.SERVICE_UNAVAILABLE, options); }; /** * The HyperText Transfer Protocol (HTTP) 504 Gateway Timeout server error response code indicates * that the server, while acting as a gateway or proxy, did not get a response in time from the * upstream server that it needed in order to complete the request. * @param uuid - UUID V4, unique identifier for this particular occurrence of the problem * @param message - Human-readable explanation specific to this occurrence of the problem * @param options - Specific options for enhanced error management * @public */ BoomHelpers.gatewayTimeout = (message, uuid, options) => { return regularError(message, uuid, HTTPCode_t_1.HTTPCode.GATEWAY_TIMEOUT, options); }; function regularError(message, uuid, code, options) { return new BoomError_1.Boom(message, uuid, code, options); } //# sourceMappingURL=BoomHelpers.js.map