@@ -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 */
8990export 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. */
208249type 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