Skip to content
Snippets Groups Projects
Verified Commit f99352e1 authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

feat: new DataFetchError

parent b39497be
No related branches found
No related tags found
No related merge requests found
...@@ -9,6 +9,7 @@ import { internalSymbol, instanceSymbol } from "../../../constants.mjs"; ...@@ -9,6 +9,7 @@ import { internalSymbol, instanceSymbol } from "../../../constants.mjs";
import {isObject, isFunction} from "../../../types/is.mjs"; import {isObject, isFunction} from "../../../types/is.mjs";
import {Server} from "../server.mjs"; import {Server} from "../server.mjs";
import {WriteError} from "./restapi/writeerror.mjs"; import {WriteError} from "./restapi/writeerror.mjs";
import {DataFetchError} from "./restapi/data-fetch-error.mjs";
export {RestAPI}; export {RestAPI};
...@@ -188,7 +189,7 @@ function fetchData(init, key, callback) { ...@@ -188,7 +189,7 @@ function fetchData(init, key, callback) {
const acceptedStatus = self.getOption(`${key}.acceptedStatus`, [200]); const acceptedStatus = self.getOption(`${key}.acceptedStatus`, [200]);
if (acceptedStatus.indexOf(resp.status) === -1) { 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(); return resp.text();
...@@ -205,7 +206,7 @@ function fetchData(init, key, callback) { ...@@ -205,7 +206,7 @@ function fetchData(init, key, callback) {
body = `${body.substring(0, 97)}...`; 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)) { if (callback && isFunction(callback)) {
......
/**
* 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"];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment