barracuda-client-api
Version:
API Client to connect to Barracuda Enterprise Service Bus
80 lines (66 loc) • 2.31 kB
text/typescript
import { IBarracudaBridgeMessage, BarracudaConnectionStatus, BarracudaConnectionProps } from "../IBarracudaClient";
export interface IBarracudaErrorPayload {
msg?: IBarracudaBridgeMessage | string | object;
requestMsg?: IBarracudaBridgeMessage;
httpRequest?: { info: RequestInfo, init: RequestInit };
httpResponse?: Response | any;
httpResponseBody?: string;
url?: string;
inboundQueueLength?: number;
isServerError?: boolean;
originalError?: Error;
connectionProps?: Pick<BarracudaConnectionProps, "endpoint" & "reconnect">;
connectionState?: BarracudaConnectionStatus;
handlerName?: string;
handlerContext?: any;
reason?: string;
instance?: string;
topic?: string;
connectedAtLeastOnce?: boolean;
}
export class BarracudaError extends Error {
// private static _constructor = (() => {
// // "this" cannot be used here
//
// const originalToString = Error.prototype.toString;
// Error.prototype.toString = function (): string {
// if (this instanceof BarracudaError) {
// let error = this as BarracudaError;
// return `[${error.name}] ${originalToString()} ==> data:${JSON.stringify(error.data)}`;
// } else {
// return originalToString();
// }
// };
//
// })();
private readonly _data: Partial<IBarracudaErrorPayload>;
public constructor(msg: string, errorData: IBarracudaErrorPayload) {
super(msg);
// Maintains proper stack trace for where our error was thrown (only available on V8)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, BarracudaError);
}
this.name = "BarracudaError";
// Custom debugging information
this.message = msg;
this._data = errorData;
}
public get data(): Partial<IBarracudaErrorPayload> {
return this._data;
}
public static isBarracudaError(ob: any): boolean {
if (typeof ob !== "object") {
return false;
}
if (!BarracudaError.prototype.isPrototypeOf(ob)) {
return false;
}
return ob.name === BarracudaError.prototype.name;
}
public static isBarracudaServerError(ob: any): boolean {
return BarracudaError.isBarracudaError(ob) && !!ob.data?.isServerError;
}
public toString(): string {
return super.toString() + " " + JSON.stringify(this.data);
}
}