diff --git a/application/source/data/datasource/server/restapi.mjs b/application/source/data/datasource/server/restapi.mjs index 6550ac2de95735bb810d0f38a8878fdec9875066..fcb5f47964c9313f8345e16f817e323e6b6e1583 100644 --- a/application/source/data/datasource/server/restapi.mjs +++ b/application/source/data/datasource/server/restapi.mjs @@ -5,12 +5,13 @@ * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html */ -import { internalSymbol, instanceSymbol } from "../../../constants.mjs"; -import { isObject, isFunction } from "../../../types/is.mjs"; -import { Server } from "../server.mjs"; -import { WriteError } from "./restapi/writeerror.mjs"; +import {internalSymbol, instanceSymbol} from "../../../constants.mjs"; +import {isObject, isFunction} from "../../../types/is.mjs"; +import {Server} from "../server.mjs"; +import {WriteError} from "./restapi/writeerror.mjs"; +import {DataFetchError} from "./restapi/data-fetch-error.mjs"; -export { RestAPI }; +export {RestAPI}; /** * @type {symbol} @@ -188,7 +189,7 @@ function fetchData(init, key, callback) { const acceptedStatus = self.getOption(`${key}.acceptedStatus`, [200]); if (acceptedStatus.indexOf(resp.status) === -1) { - throw Error(`the data cannot be ${key} (response ${resp.status})`); + throw new DataFetchError(`the response does not contain a accepted status (actual: ${resp.status}).`, response); } return resp.text(); @@ -205,7 +206,7 @@ function fetchData(init, key, callback) { body = `${body.substring(0, 97)}...`; } - throw new Error(`the response does not contain a valid json (actual: ${body}).`); + throw new DataFetchError(`the response does not contain a valid json (actual: ${body}).`, response); } if (callback && isFunction(callback)) { diff --git a/application/source/data/datasource/server/restapi/data-fetch-error.mjs b/application/source/data/datasource/server/restapi/data-fetch-error.mjs new file mode 100644 index 0000000000000000000000000000000000000000..1c99f6d8ed7701320a059605137e038253a8c1e9 --- /dev/null +++ b/application/source/data/datasource/server/restapi/data-fetch-error.mjs @@ -0,0 +1,50 @@ +/** + * Copyright schukai GmbH and contributors 2023. All Rights Reserved. + * Node module: @schukai/monster + * This file is licensed under the AGPLv3 License. + * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html + */ + +import { internalSymbol, instanceSymbol } from "../../../../constants.mjs"; + +export { DataFetchError }; + +/** + * Error message for API requests + * + * @license AGPLv3 + * @since 3.43.0 + * @copyright schukai GmbH + * @memberOf Monster.Data.Datasource.Server.RestAPI + * @summary the error is thrown by the rest api in case of error + */ +class DataFetchError extends Error { + /** + * + * @param {string} message + * @param {Response} response + */ + constructor(message, response) { + super(message); + this[internalSymbol] = { + response: response + }; + } + + /** + * This method is called by the `instanceof` operator. + * @returns {symbol} + * @since 2.1.0 + */ + static get [instanceSymbol]() { + return Symbol.for("@schukai/monster/data/datasource/server/restapi/datafetcherror@@instance"); + } + + /** + * @return {Response} + */ + getResponse() { + return this[internalSymbol]["response"]; + } + +}