nav-connect
Version:
Nav Online Invoice connection module
115 lines • 3.87 kB
JavaScript
/**
* Base error class for all NAV API errors.
*/
export class NavApiError extends Error {
cause;
constructor(message, cause) {
super(message);
this.cause = cause;
this.name = "NavApiError";
}
}
/**
* Thrown when the HTTP request times out (httpTimeoutMs exceeded).
*/
export class NavApiTimeoutError extends NavApiError {
timeoutMs;
constructor(timeoutMs) {
super(`NAV API request timed out after ${timeoutMs}ms`);
this.name = "NavApiTimeoutError";
this.timeoutMs = timeoutMs;
}
}
/**
* Thrown when the NAV API returns an HTTP error with a parseable XML error body.
* Contains the structured funcCode, errorCode, message, and optional validation messages.
*/
export class NavApiResponseError extends NavApiError {
funcCode;
errorCode;
httpStatus;
technicalValidationMessages;
constructor(params) {
const msg = [
`NAV API error [${params.funcCode}]`,
params.errorCode ? `code: ${params.errorCode}` : null,
params.message ?? null,
]
.filter(Boolean)
.join(" - ");
super(msg);
this.name = "NavApiResponseError";
this.funcCode = params.funcCode;
this.errorCode = params.errorCode;
this.httpStatus = params.httpStatus;
this.technicalValidationMessages = params.technicalValidationMessages;
}
}
/**
* Thrown when the NAV API returns an HTTP error but the body cannot be parsed.
*/
export class NavApiHttpError extends NavApiError {
httpStatus;
statusText;
responseBody;
constructor(httpStatus, statusText, responseBody, cause) {
super(`NAV API HTTP error ${httpStatus}${statusText ? ` ${statusText}` : ""}`, cause);
this.name = "NavApiHttpError";
this.httpStatus = httpStatus;
this.statusText = statusText;
this.responseBody = responseBody;
}
}
/**
* Thrown when the generated XML fails XSD validation before sending.
*/
export class NavXmlValidationError extends NavApiError {
requestType;
validationErrors;
constructor(requestType, validationErrors) {
super(`XML validation failed for ${requestType}:\n${validationErrors.join("\n")}`);
this.name = "NavXmlValidationError";
this.requestType = requestType;
this.validationErrors = validationErrors;
}
}
/**
* Thrown when the NAV API response XML fails XSD validation during parsing.
* Wraps the XmlValidationError from nav-osa-core.
*/
export class NavResponseXmlValidationError extends NavApiError {
validationErrors;
xsdPath;
constructor(validationErrors, xsdPath) {
super(`NAV API response XML validation failed${xsdPath ? ` against ${xsdPath}` : ""}:\n${validationErrors.join("\n")}`);
this.name = "NavResponseXmlValidationError";
this.validationErrors = validationErrors;
this.xsdPath = xsdPath;
}
}
/**
* Thrown when the NavApiConfig is invalid.
*/
export class NavConfigError extends NavApiError {
validationErrors;
constructor(validationErrors) {
super(`NavApiConfig validation failed:\n${validationErrors.join("\n")}`);
this.name = "NavConfigError";
this.validationErrors = validationErrors;
}
}
/**
* Thrown when the requested date range exceeds the NAV API limit (35 days).
*/
export class NavDateRangeError extends NavApiError {
requestedDays;
maxDays;
constructor(requestedDays, maxDays = 35) {
super(`Date range too large: ${requestedDays} days requested, maximum is ${maxDays} days. ` +
`Use queryInvoiceDigestAll() to automatically split large date ranges into ${maxDays}-day chunks.`);
this.name = "NavDateRangeError";
this.requestedDays = requestedDays;
this.maxDays = maxDays;
}
}
//# sourceMappingURL=errors.js.map