Skip to content

Commit ac627ca

Browse files
fix(server): a refused GitHub REST request says GitHub's reason
REST failure bodies carry message and errors ("A pull request already exists for acme:feature"); they now reach the error message the way GraphQL errors did, instead of only "GitHub returned HTTP 422". Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent f452f90 commit ac627ca

2 files changed

Lines changed: 80 additions & 7 deletions

File tree

‎apps/server/src/sourceControl/GitHubApi.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,38 @@ describe("GitHubApi", () => {
184184
}).pipe(Effect.provide(layer));
185185
});
186186

187+
it.effect("carries GitHub's own reason for a refused REST request", () => {
188+
const { layer } = harness(() =>
189+
json(
190+
{
191+
message: "Validation Failed",
192+
errors: [
193+
{
194+
resource: "PullRequest",
195+
code: "custom",
196+
message: "A pull request already exists for acme:feature.",
197+
},
198+
],
199+
},
200+
{ status: 422 },
201+
),
202+
);
203+
return Effect.gen(function* () {
204+
const api = yield* GitHubApi.GitHubApi;
205+
const error = yield* Effect.flip(
206+
api.rest({
207+
host: "github.com",
208+
operation: "createPullRequest",
209+
method: "POST",
210+
path: "repos/acme/web/pulls",
211+
}),
212+
);
213+
expect(error.message).toBe(
214+
"GitHub returned an error: Validation Failed; A pull request already exists for acme:feature.",
215+
);
216+
}).pipe(Effect.provide(layer));
217+
});
218+
187219
it.effect("lets an interactive request through a pause a background read recorded", () => {
188220
const reset = Math.floor(NOW / 1000) + 600;
189221
let call = 0;

‎apps/server/src/sourceControl/GitHubApi.ts‎

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,22 @@ export class GitHubApiNotFoundError extends Schema.TaggedError<GitHubApiNotFound
8383
}
8484

8585
/**
86-
* GitHub answered with a failure that is none of the above. `graphqlErrors` carries GitHub's own
87-
* error messages for a GraphQL answer: they name the field and the reason, never a token.
86+
* GitHub answered with a failure that is none of the above. `githubErrors` carries GitHub's own
87+
* error messages, from a GraphQL `errors` list or a REST `message`/`errors` body: they name the
88+
* field and the reason ("A pull request already exists for acme:feature"), never a token.
8889
*/
8990
export class GitHubApiResponseError extends Schema.TaggedError<GitHubApiResponseError>()(
9091
"GitHubApiResponseError",
9192
{
9293
host: Schema.String,
9394
operation: Schema.String,
9495
status: Schema.Int,
95-
graphqlErrors: Schema.optionalKey(Schema.Array(Schema.String)),
96+
githubErrors: Schema.optionalKey(Schema.Array(Schema.String)),
9697
},
9798
) {
9899
override get message(): string {
99-
return this.graphqlErrors !== undefined && this.graphqlErrors.length > 0
100-
? `GitHub returned an error: ${this.graphqlErrors.join("; ")}`
100+
return this.githubErrors !== undefined && this.githubErrors.length > 0
101+
? `GitHub returned an error: ${this.githubErrors.join("; ")}`
101102
: `GitHub returned HTTP ${this.status}.`;
102103
}
103104
}
@@ -204,6 +205,46 @@ const decodeGraphQlErrors = Schema.decodeUnknownOption(
204205
),
205206
);
206207

208+
/** A REST failure body: `{ message, errors: [{ message } | { resource, field, code }] }`. */
209+
const decodeRestErrors = Schema.decodeUnknownOption(
210+
Schema.fromJsonString(
211+
Schema.Struct({
212+
message: Schema.optional(Schema.String),
213+
errors: Schema.optional(
214+
Schema.Array(
215+
Schema.Union([
216+
Schema.String,
217+
Schema.Struct({
218+
message: Schema.optional(Schema.String),
219+
field: Schema.optional(Schema.String),
220+
code: Schema.optional(Schema.String),
221+
}),
222+
]),
223+
),
224+
),
225+
}),
226+
),
227+
);
228+
229+
function restErrorMessages(body: string): ReadonlyArray<string> | undefined {
230+
return Option.match(decodeRestErrors(body), {
231+
onNone: () => undefined,
232+
onSome: (decoded) => {
233+
const details = (decoded.errors ?? []).flatMap((error) =>
234+
typeof error === "string"
235+
? [error]
236+
: error.message !== undefined
237+
? [error.message]
238+
: error.field !== undefined && error.code !== undefined
239+
? [`${error.field} ${error.code}`]
240+
: [],
241+
);
242+
const messages = [...(decoded.message === undefined ? [] : [decoded.message]), ...details];
243+
return messages.length > 0 ? messages : undefined;
244+
},
245+
});
246+
}
247+
207248
/** What one GitHub answer means, decided once from its status, headers and body. */
208249
type Answer = Data.TaggedEnum<{
209250
Ok: {};
@@ -253,7 +294,7 @@ function classify(input: {
253294
if ((status >= 200 && status < 300) || (status === 304 && input.acceptNotModified)) {
254295
return Answer.Ok();
255296
}
256-
return Answer.Failed({ messages: undefined });
297+
return Answer.Failed({ messages: input.graphql ? undefined : restErrorMessages(body) });
257298
}
258299

259300
/** @public Service construction is part of the canonical Effect module API. */
@@ -377,7 +418,7 @@ export const make = Effect.gen(function* () {
377418
new GitHubApiResponseError({
378419
...context,
379420
status,
380-
...(messages === undefined ? {} : { graphqlErrors: messages }),
421+
...(messages === undefined ? {} : { githubErrors: messages }),
381422
}),
382423
),
383424
},

0 commit comments

Comments
 (0)