-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#3613: fix cause on RemoteServiceError
- Loading branch information
1 parent
222e735
commit bfdd9a4
Showing
5 changed files
with
125 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright (C) 2022 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { BusinessError } from "@/errors/businessErrors"; | ||
import { serializeError } from "serialize-error"; | ||
|
||
describe("BusinessError", () => { | ||
it("records cause", () => { | ||
const cause = new Error("Pylon Error"); | ||
const error = new BusinessError("You Must Construct Additional Pylons", { | ||
cause, | ||
}); | ||
expect(serializeError(error).cause).toStrictEqual(serializeError(cause)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright (C) 2022 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { serializeError } from "serialize-error"; | ||
import { | ||
ClientRequestError, | ||
RemoteServiceError, | ||
} from "@/errors/clientRequestErrors"; | ||
import axios, { AxiosError } from "axios"; | ||
import MockAdapter from "axios-mock-adapter"; | ||
|
||
const axiosMock = new MockAdapter(axios); | ||
|
||
afterEach(() => { | ||
axiosMock.reset(); | ||
axiosMock.resetHistory(); | ||
}); | ||
|
||
describe("RemoteServiceError", () => { | ||
it("records cause", () => { | ||
const cause = new AxiosError("The tubes are clogged"); | ||
// `serializeError` uses the name/message/stack property to determine if it's errorLike | ||
// In practice, AxiosErrors actually have stacks: https://github.com/axios/axios/issues/2387 | ||
cause.stack = ""; | ||
const error = new RemoteServiceError( | ||
"You Must Construct Additional Pylons", | ||
{ cause } | ||
); | ||
expect(serializeError(error, { useToJSON: false }).cause).toStrictEqual( | ||
serializeError(cause, { useToJSON: false }) | ||
); | ||
}); | ||
|
||
it("keeps request and response", async () => { | ||
axiosMock | ||
.onAny() | ||
.reply(400, { detail: "I'm sorry Dave, I'm afraid I can't do that" }); | ||
|
||
let cause: AxiosError; | ||
|
||
try { | ||
await axios({ | ||
url: "https://httpstat.us/400", | ||
method: "get", | ||
}); | ||
} catch (error: unknown) { | ||
cause = error as AxiosError; | ||
} | ||
|
||
const error = new RemoteServiceError( | ||
"You Must Construct Additional Pylons", | ||
{ cause } | ||
); | ||
|
||
const serialized = serializeError(error, { useToJSON: false }); | ||
|
||
expect((serialized.cause as AxiosError).config).toBeTruthy(); | ||
expect((serialized.cause as AxiosError).response).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
describe("ClientRequestError", () => { | ||
it("records cause", () => { | ||
const cause = new AxiosError("The tubes are clogged"); | ||
cause.stack = ""; | ||
// `serializeError` uses the name/message/stack property to determine if it's errorLike | ||
// In practice, AxiosErrors actually have stacks: https://github.com/axios/axios/issues/2387 | ||
const error = new ClientRequestError( | ||
"You Must Construct Additional Pylons", | ||
{ cause } | ||
); | ||
expect(serializeError(error, { useToJSON: false }).cause).toStrictEqual( | ||
serializeError(cause, { useToJSON: false }) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters