Skip to content

Commit eb45e32

Browse files
authored
feat: add server validation errors to error message (#91)
2 parents 0fc38c0 + 5795230 commit eb45e32

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/fetch-wrapper.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,14 @@ export default function fetchWrapper(
7676
});
7777

7878
try {
79-
Object.assign(error, JSON.parse(error.message));
79+
let responseBody = JSON.parse(error.message);
80+
Object.assign(error, responseBody);
81+
82+
let errors = responseBody.errors;
83+
84+
// Assumption `errors` would always be in Array Fotmat
85+
error.message =
86+
error.message + ": " + errors.map(JSON.stringify).join(", ");
8087
} catch (e) {
8188
// ignore, see octokit/rest.js#684
8289
}

test/request.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,4 +691,61 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
691691
expect(response.data).toEqual("ok");
692692
});
693693
});
694+
695+
it("@octokit/rest.js/issues/1497/error-messages-on-validation-error", function() {
696+
const mock = fetchMock.sandbox().mock(
697+
"https://request-errors-test.com/repos/gr2m/sandbox/branches/gr2m-patch-1/protection",
698+
{
699+
status: 400,
700+
body: {
701+
message: "Validation failed",
702+
errors: [
703+
"Only organization repositories can have users and team restrictions",
704+
{ resource: "Search", field: "q", code: "invalid" }
705+
],
706+
documentation_url:
707+
"https://developer.github.com/v3/repos/branches/#update-branch-protection"
708+
}
709+
},
710+
{
711+
method: "PUT",
712+
headers: {
713+
accept: "application/vnd.github.luke-cage-preview+json",
714+
authorization: "token secret123"
715+
}
716+
}
717+
);
718+
719+
return request("PUT /repos/:owner/:repo/branches/:branch/protection", {
720+
baseUrl: "https://request-errors-test.com",
721+
mediaType: { previews: ["luke-cage"] },
722+
headers: {
723+
authorization: "token secret123"
724+
},
725+
owner: "gr2m",
726+
repo: "sandbox",
727+
branch: "gr2m-patch-1",
728+
required_status_checks: { strict: true, contexts: ["wip"] },
729+
enforce_admins: true,
730+
required_pull_request_reviews: {
731+
required_approving_review_count: 1,
732+
dismiss_stale_reviews: true,
733+
require_code_owner_reviews: true,
734+
dismissal_restrictions: { users: [], teams: [] }
735+
},
736+
restrictions: { users: [], teams: [] },
737+
request: {
738+
fetch: mock
739+
}
740+
})
741+
.then(() => {
742+
fail("This should return error.");
743+
})
744+
.catch(error => {
745+
expect(error).toHaveProperty(
746+
"message",
747+
`Validation failed: "Only organization repositories can have users and team restrictions", {"resource":"Search","field":"q","code":"invalid"}`
748+
);
749+
});
750+
});
694751
});

0 commit comments

Comments
 (0)