Skip to content

Commit 55e9cd5

Browse files
juliusmarmingecodex
andcommitted
Derive environment HTTP error messages
Co-authored-by: codex <[email protected]>
1 parent 62db448 commit 55e9cd5

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { assert, describe, it } from "@effect/vitest";
2+
3+
import {
4+
EnvironmentAuthInvalidError,
5+
EnvironmentInternalError,
6+
EnvironmentOperationForbiddenError,
7+
EnvironmentRequestInvalidError,
8+
EnvironmentScopeRequiredError,
9+
} from "./environmentHttp.ts";
10+
11+
describe("environment HTTP common errors", () => {
12+
it("derives messages from structural diagnostics", () => {
13+
assert.equal(
14+
new EnvironmentRequestInvalidError({
15+
code: "invalid_request",
16+
reason: "invalid_scope",
17+
traceId: "trace-request",
18+
}).message,
19+
"Environment request rejected with invalid_request (invalid_scope; trace trace-request).",
20+
);
21+
assert.equal(
22+
new EnvironmentAuthInvalidError({
23+
code: "auth_invalid",
24+
reason: "invalid_credential",
25+
traceId: "trace-auth",
26+
}).message,
27+
"Environment authentication rejected with auth_invalid (invalid_credential; trace trace-auth).",
28+
);
29+
assert.equal(
30+
new EnvironmentScopeRequiredError({
31+
code: "insufficient_scope",
32+
requiredScope: "access:read",
33+
traceId: "trace-scope",
34+
}).message,
35+
"Environment authorization requires scope access:read (insufficient_scope; trace trace-scope).",
36+
);
37+
assert.equal(
38+
new EnvironmentOperationForbiddenError({
39+
code: "operation_forbidden",
40+
reason: "current_session_revoke_not_allowed",
41+
traceId: "trace-forbidden",
42+
}).message,
43+
"Environment operation rejected with operation_forbidden (current_session_revoke_not_allowed; trace trace-forbidden).",
44+
);
45+
assert.equal(
46+
new EnvironmentInternalError({
47+
code: "internal_error",
48+
reason: "access_token_issuance_failed",
49+
traceId: "trace-internal",
50+
}).message,
51+
"Environment request failed with internal_error (access_token_issuance_failed; trace trace-internal).",
52+
);
53+
});
54+
});

‎packages/contracts/src/environmentHttp.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ export class EnvironmentRequestInvalidError extends Schema.TaggedErrorClass<Envi
9494
},
9595
{ httpApiStatus: 400 },
9696
) {
97+
override get message(): string {
98+
return `Environment request rejected with ${this.code} (${this.reason}; trace ${this.traceId}).`;
99+
}
100+
97101
[HttpServerRespondable.symbol]() {
98102
return HttpServerResponse.schemaJson(EnvironmentRequestInvalidError)(this, { status: 400 });
99103
}
@@ -108,6 +112,10 @@ export class EnvironmentAuthInvalidError extends Schema.TaggedErrorClass<Environ
108112
},
109113
{ httpApiStatus: 401 },
110114
) {
115+
override get message(): string {
116+
return `Environment authentication rejected with ${this.code} (${this.reason}; trace ${this.traceId}).`;
117+
}
118+
111119
[HttpServerRespondable.symbol]() {
112120
return HttpServerResponse.schemaJson(EnvironmentAuthInvalidError)(this, { status: 401 });
113121
}
@@ -122,6 +130,10 @@ export class EnvironmentScopeRequiredError extends Schema.TaggedErrorClass<Envir
122130
},
123131
{ httpApiStatus: 403 },
124132
) {
133+
override get message(): string {
134+
return `Environment authorization requires scope ${this.requiredScope} (${this.code}; trace ${this.traceId}).`;
135+
}
136+
125137
[HttpServerRespondable.symbol]() {
126138
return HttpServerResponse.schemaJson(EnvironmentScopeRequiredError)(this, { status: 403 });
127139
}
@@ -136,6 +148,10 @@ export class EnvironmentOperationForbiddenError extends Schema.TaggedErrorClass<
136148
},
137149
{ httpApiStatus: 403 },
138150
) {
151+
override get message(): string {
152+
return `Environment operation rejected with ${this.code} (${this.reason}; trace ${this.traceId}).`;
153+
}
154+
139155
[HttpServerRespondable.symbol]() {
140156
return HttpServerResponse.schemaJson(EnvironmentOperationForbiddenError)(this, { status: 403 });
141157
}
@@ -150,6 +166,10 @@ export class EnvironmentInternalError extends Schema.TaggedErrorClass<Environmen
150166
},
151167
{ httpApiStatus: 500 },
152168
) {
169+
override get message(): string {
170+
return `Environment request failed with ${this.code} (${this.reason}; trace ${this.traceId}).`;
171+
}
172+
153173
[HttpServerRespondable.symbol]() {
154174
return HttpServerResponse.schemaJson(EnvironmentInternalError)(this, { status: 500 });
155175
}

0 commit comments

Comments
 (0)