From 07336e929fd0e9deded962482dbecc47d1956dc2 Mon Sep 17 00:00:00 2001 From: Marcin Lewandowski Date: Wed, 25 Sep 2024 22:26:19 +0200 Subject: [PATCH] RDBC-873 Fix handling server failure in 6.0.0 client --- src/Exceptions/index.ts | 16 +++++++++++- src/Http/RequestExecutor.ts | 2 +- test/ExceptionTest.ts | 49 +++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 test/ExceptionTest.ts diff --git a/src/Exceptions/index.ts b/src/Exceptions/index.ts index ba97eb80..a5362912 100644 --- a/src/Exceptions/index.ts +++ b/src/Exceptions/index.ts @@ -17,6 +17,19 @@ export function throwError( throw getError(errName, message, errCause, info); } +function buildMessageWithInner(error: any) { + if (!error) { + return null; + } + if (!error.cause) { + return error.message; + } + + const inner = buildMessageWithInner(error.cause); + return inner ? error.message + ": " + inner : error.message; +} + + export function getError(errName: RavenErrorType, message: string): Error; export function getError( errName: string, @@ -32,7 +45,8 @@ export function getError( message: string = "", errCause?: Error, info?: { [key: string]: any }): Error { - const error = new Error(message + (errCause ? ": " + errCause.message : ""), { cause: errCause }); + const innerMessage = buildMessageWithInner(errCause); + const error = new Error(message + (innerMessage ? ": " + innerMessage : ""), { cause: errCause }); error.name = errName; if (info) { diff --git a/src/Http/RequestExecutor.ts b/src/Http/RequestExecutor.ts index 9d876f54..a009bff6 100644 --- a/src/Http/RequestExecutor.ts +++ b/src/Http/RequestExecutor.ts @@ -1924,7 +1924,7 @@ export class RequestExecutor implements IDisposable { const exceptionSchema = { url: req.uri.toString(), message: e.message, - error: `An exception occurred while contacting ${ req.uri }: ${ e.message } . ${ EOL + e.stack }`, + error: `An exception occurred while contacting ${ req.uri }: ${ e.message }`, type: e.name }; diff --git a/test/ExceptionTest.ts b/test/ExceptionTest.ts new file mode 100644 index 00000000..6c7c679e --- /dev/null +++ b/test/ExceptionTest.ts @@ -0,0 +1,49 @@ +import { inspect } from "node:util"; +import { assertThat } from "./Utils/AssertExtensions.js"; +import { getError } from "../src/Exceptions/index.js"; + +const inRush = new Error("I was in rush"); +const glasses = new Error("I forgot to put my classes", { cause: inRush }); +const accident = new Error("I caused accident", { cause: glasses }); + + +describe("Exception Test", () => { + + it("can see nested causes", () => { + const exceptionSerialized = inspect(accident); + + assertThat(exceptionSerialized) + .contains(inRush.message); + + assertThat(exceptionSerialized) + .contains(glasses.message); + + assertThat(exceptionSerialized) + .contains(accident); + }); + + it("can get error - no cause", () => { + const error = getError("ConflictException", "This is fake conflict"); + const exceptionSerialized = inspect(error); + assertThat(exceptionSerialized) + .contains(error.message); + }); + + it("can get error - with cause", () => { + const error = getError("ConflictException", "This is fake conflict", accident); + + const exceptionSerialized = inspect(error); + + assertThat(exceptionSerialized) + .contains(error.message); + + assertThat(exceptionSerialized) + .contains(inRush.message); + + assertThat(exceptionSerialized) + .contains(glasses.message); + + assertThat(exceptionSerialized) + .contains(accident); + }); +}) \ No newline at end of file