@t3ned/channel
Version:
Ergonomic, chaining-based Typescript framework for quick API development for Fastify
139 lines • 3.27 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiError = void 0;
const http_1 = require("http");
const _1 = require(".");
class ApiError extends _1.ChannelError {
constructor() {
super(...arguments);
/**
* The internal application error code
*/
Object.defineProperty(this, "code", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* The error data
*/
Object.defineProperty(this, "data", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* The error trace
*/
Object.defineProperty(this, "trace", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* The HTTP status code
*/
Object.defineProperty(this, "_status", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* The error message
*/
Object.defineProperty(this, "_message", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
}
/**
* Set the HTTP status code
* @param status The status
*
* @returns The ApiError
*/
setStatus(status) {
this._status = status;
if (!this._message) {
const statusMessage = http_1.STATUS_CODES[this._status];
if (statusMessage)
return this.setMessage(statusMessage);
}
return this;
}
/**
* Set the internal application error code
* @param code The code
*
* @returns The ApiError
*/
setCode(code) {
this.code = code;
return this;
}
/**
* Set the error message
* @param message The message
*
* @returns The ApiError
*/
setMessage(message) {
this._message = message;
return this;
}
/**
* Set the error trace
* @param trace The trace
*
* @returns The ApiError
*/
setTrace(trace) {
this.trace = trace;
return this;
}
/**
* Set the error data
* @param data The data
*
* @returns The ApiError
*/
setData(data) {
this.data = data;
return this;
}
/**
* The HTTP status code
*/
get status() {
if (!this._status)
throw new _1.ChannelError("Missing `status` field");
return this._status;
}
/**
* The error message
*/
get message() {
if (!this._message)
throw new _1.ChannelError("Missing `message` field");
return this._message;
}
/**
* @returns Get the JSON representation of the ApiError
*/
toJSON() {
return {
code: this.code,
message: this.message,
data: this.data,
trace: this.trace,
};
}
}
exports.ApiError = ApiError;
//# sourceMappingURL=ApiError.js.map