Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/errors/PlaneApiError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ export class PlaneApiError extends PlaneError {
public errors?: FieldError[];

constructor(problem: ProblemDetail) {
super(`HTTP ${problem.status} ${problem.code}: ${problem.detail}`, problem.status, problem);
// The field messages say why; `detail` is often a generic "fields failed validation".
const reasons = (problem.errors ?? []).map((error) => `${error.field}: ${error.message}`).join("; ");
super(
`HTTP ${problem.status} ${problem.code}: ${problem.detail}${reasons ? ` (${reasons})` : ""}`,
problem.status,
problem
);
this.name = "PlaneApiError";
this.status = problem.status;
this.type = problem.type;
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/v2/errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { PlaneApiError } from "../../../src/errors/PlaneApiError";

describe("PlaneApiError message", () => {
it("ends with the field messages, which say why the request was refused", () => {
const error = PlaneApiError.fromPayload(400, {
code: "invalid_request",
detail: "One or more fields failed validation.",
errors: [
{ field: "role", message: "Workspace guests can only be assigned commenter or guest roles on projects" },
{ field: "member_id", message: "Not a member of this workspace." },
],
});

expect(error.message).toBe(
"HTTP 400 invalid_request: One or more fields failed validation. " +
"(role: Workspace guests can only be assigned commenter or guest roles on projects; " +
"member_id: Not a member of this workspace.)"
);
expect(error.errors).toHaveLength(2);
});

it("keeps the plain message when there are no field errors", () => {
const error = PlaneApiError.fromPayload(404, { code: "not_found", detail: "Not found." });

expect(error.message).toBe("HTTP 404 not_found: Not found.");
});
});
Loading