Skip to content

Commit

Permalink
Merge pull request #464 from ml054/RDBC-873
Browse files Browse the repository at this point in the history
RDBC-873 Fix handling server failure in 6.0.0 client
  • Loading branch information
ml054 authored Sep 26, 2024
2 parents d1ac6ce + 07336e9 commit b82a209
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/Exceptions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/Http/RequestExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
49 changes: 49 additions & 0 deletions test/ExceptionTest.ts
Original file line number Diff line number Diff line change
@@ -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);
});
})

0 comments on commit b82a209

Please sign in to comment.