27 lines
854 B
JavaScript
27 lines
854 B
JavaScript
class RrApiError extends Error {
|
|
constructor(message, { reqId, url, apiName, errorData, status, statusText } = {}) {
|
|
super(message);
|
|
this.name = "RrApiError";
|
|
this.reqId = reqId;
|
|
this.url = url;
|
|
this.apiName = apiName;
|
|
this.errorData = errorData;
|
|
this.status = status;
|
|
this.statusText = statusText;
|
|
}
|
|
}
|
|
|
|
// Match Rome/RR envelope once you confirm it; keep this central.
|
|
function assertRrOk(data, { apiName, allowEmpty = false } = {}) {
|
|
// Example heuristics — update to exact envelope from the PDF:
|
|
// - successFlag === true, or
|
|
// - code === "0", or
|
|
// - !error / !errors length, etc.
|
|
if (!allowEmpty && (data == null || data.error || data.errors?.length)) {
|
|
throw new RrApiError(`${apiName} returned an error`, { errorData: data });
|
|
}
|
|
return data;
|
|
}
|
|
|
|
module.exports = { RrApiError, assertRrOk };
|