UNPKG

mindee

Version:

Mindee Client Library for Node.js

212 lines (211 loc) 7.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MindeeHttp504Error = exports.MindeeHttp500Error = exports.MindeeHttp429Error = exports.MindeeHttp413Error = exports.MindeeHttp404Error = exports.MindeeHttp403Error = exports.MindeeHttp401Error = exports.MindeeHttp400Error = exports.MindeeHttpError = void 0; exports.handleError = handleError; const errors_1 = require("../errors"); const handler_1 = require("../errors/handler"); function handleError(urlName, response, serverError) { let code; try { if (response.data.api_request["status_code"] === 200 && response.data?.job?.error?.code) { code = 500; response.data.api_request.error = response.data.job.error; } else if (response.data) { code = response.data.api_request["status_code"]; } } catch { code = 500; } let errorObj; try { //Regular instances where the returned error is in JSON format. errorObj = response.data.api_request.error; } catch { //Rare instances where errors are returned as HTML instead of JSON. if (!("reconstructedResponse" in response.data)) { response.data.reconstructedResponse = ""; } if (response.data.reconstructedResponse.includes("Maximum pdf pages")) { errorObj = { message: "TooManyPages", details: "Maximum amount of pdf pages reached.", }; } else if (response.data.reconstructedResponse.includes("Max file size is")) { errorObj = { message: "FileTooLarge", details: "Maximum file size reached.", }; } else if (response.data.reconstructedResponse.includes("Invalid file type")) { errorObj = { message: "InvalidFiletype", details: "Invalid file type.", }; } else if (response.data.reconstructedResponse.includes("Gateway timeout")) { errorObj = { message: "RequestTimeout", details: "Request timed out.", }; } else if (response.data.reconstructedResponse.includes("Bad gateway")) { errorObj = { message: "BadRequest", details: "Bad Gateway", }; } else if (response.data.reconstructedResponse.includes("Too Many Requests")) { errorObj = { message: "TooManyRequests", details: "Too Many Requests.", }; } else { errorObj = { message: "Unknown Server Error.", details: response.data.reconstructedResponse, }; } } if (serverError !== undefined && (!("message" in errorObj) || !errorObj.message || errorObj.message.length === 0)) { errorObj.message = serverError; } let errorToThrow; switch (code) { case 400: errorToThrow = new MindeeHttp400Error(errorObj, urlName, code); break; case 401: errorToThrow = new MindeeHttp401Error(errorObj, urlName, code); break; case 403: errorToThrow = new MindeeHttp403Error(errorObj, urlName, code); break; case 404: errorToThrow = new MindeeHttp404Error(errorObj, urlName, code); break; case 413: errorToThrow = new MindeeHttp413Error(errorObj, urlName, code); break; case 429: errorToThrow = new MindeeHttp429Error(errorObj, urlName, code); break; case 500: errorToThrow = new MindeeHttp500Error(errorObj, urlName, code); break; case 504: errorToThrow = new MindeeHttp504Error(errorObj, urlName, code); break; default: errorToThrow = new MindeeHttpError(errorObj, urlName, code); break; } handler_1.errorHandler.throw(errorToThrow); } /** * `Error` wrapper for server (HTTP) errors. * Is used when an error is lacking a handled error code. */ class MindeeHttpError extends errors_1.MindeeError { constructor(httpError, urlName, code) { super(`${urlName} API ${code} HTTP error: ${httpError.message}`); /** Description of the error. */ this.message = ""; /** Additional details on the error. */ this.details = ""; this.details = httpError.details; this.message = httpError.message; this.code = code; this.name = "MindeeHttpError"; } } exports.MindeeHttpError = MindeeHttpError; /** * Generic client errors. * Can include errors like InvalidQuery. */ class MindeeHttp400Error extends MindeeHttpError { constructor(httpError, urlName, code) { super(httpError, urlName, code); this.name = "MindeeHttp400Error"; } } exports.MindeeHttp400Error = MindeeHttp400Error; /** * Can include errors like NoTokenSet or InvalidToken. */ class MindeeHttp401Error extends MindeeHttpError { constructor(httpError, urlName, code) { super(httpError, urlName, code); this.name = "MindeeHttp401Error"; } } exports.MindeeHttp401Error = MindeeHttp401Error; /** * Regular AccessForbidden error. * Can also include errors like PlanLimitReached, AsyncRequestDisallowed or SyncRequestDisallowed. */ class MindeeHttp403Error extends MindeeHttpError { constructor(httpError, urlName, code) { super(httpError, urlName, code); this.name = "MindeeHttp403Error"; } } exports.MindeeHttp403Error = MindeeHttp403Error; class MindeeHttp404Error extends MindeeHttpError { constructor(httpError, urlName, code) { super(httpError, urlName, code); this.name = "MindeeHttp404Error"; } } exports.MindeeHttp404Error = MindeeHttp404Error; /** * Rare error. * Can occasionally happen when unusually large documents are passed. */ class MindeeHttp413Error extends MindeeHttpError { constructor(httpError, urlName, code) { super(httpError, urlName, code); this.name = "MindeeHttp413Error"; } } exports.MindeeHttp413Error = MindeeHttp413Error; /** * Usually corresponds to TooManyRequests errors. * Arises whenever too many calls to the API are made in quick succession. */ class MindeeHttp429Error extends MindeeHttpError { constructor(httpError, urlName, code) { super(httpError, urlName, code); this.name = "MindeeHttp429Error"; } } exports.MindeeHttp429Error = MindeeHttp429Error; /** * Generic server errors. */ class MindeeHttp500Error extends MindeeHttpError { constructor(httpError, urlName, code) { super(httpError, urlName, code); this.name = "MindeeHttp500Error"; } } exports.MindeeHttp500Error = MindeeHttp500Error; /** * Miscellaneous server errors. * Can include errors like RequestTimeout or GatewayTimeout. */ class MindeeHttp504Error extends MindeeHttpError { constructor(httpError, urlName, code) { super(httpError, urlName, code); this.name = "MindeeHttp504Error"; } } exports.MindeeHttp504Error = MindeeHttp504Error;