From f99352e18c0a3564879745c1d685970d85fe8a7e Mon Sep 17 00:00:00 2001 From: Volker Schukai <volker.schukai@schukai.com> Date: Fri, 7 Apr 2023 19:53:18 +0200 Subject: [PATCH] feat: new DataFetchError --- .../source/data/datasource/server/restapi.mjs | 15 +++--- .../server/restapi/data-fetch-error.mjs | 50 +++++++++++++++++++ 2 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 application/source/data/datasource/server/restapi/data-fetch-error.mjs diff --git a/application/source/data/datasource/server/restapi.mjs b/application/source/data/datasource/server/restapi.mjs index 6550ac2de..fcb5f4796 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 000000000..1c99f6d8e --- /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"]; + } + +} -- GitLab