|
| 1 | +import { RequestError } from "@octokit/request-error"; |
| 2 | +import { createMockLogger } from "../../__mocks__/loggerMock"; |
| 3 | +import { handleRequestError } from "../../../src/variant-analysis/custom-errors"; |
| 4 | +import { faker } from "@faker-js/faker"; |
| 5 | + |
| 6 | +describe("handleRequestError", () => { |
| 7 | + const logger = createMockLogger(); |
| 8 | + |
| 9 | + it("returns false when handling a non-422 error", () => { |
| 10 | + const e = mockRequestError(404, { |
| 11 | + message: "Not Found", |
| 12 | + }); |
| 13 | + expect(handleRequestError(e, logger)).toBe(false); |
| 14 | + expect(logger.showErrorMessage).not.toHaveBeenCalled(); |
| 15 | + }); |
| 16 | + |
| 17 | + it("returns false when handling a different error without errors", () => { |
| 18 | + const e = mockRequestError(422, { |
| 19 | + message: |
| 20 | + "Unable to trigger a variant analysis. None of the requested repositories could be found.", |
| 21 | + }); |
| 22 | + expect(handleRequestError(e, logger)).toBe(false); |
| 23 | + expect(logger.showErrorMessage).not.toHaveBeenCalled(); |
| 24 | + }); |
| 25 | + |
| 26 | + it("returns false when handling an error without response body", () => { |
| 27 | + const e = mockRequestError(422, undefined); |
| 28 | + expect(handleRequestError(e, logger)).toBe(false); |
| 29 | + expect(logger.showErrorMessage).not.toHaveBeenCalled(); |
| 30 | + }); |
| 31 | + |
| 32 | + it("returns false when handling an error without response", () => { |
| 33 | + const e = new RequestError("Timeout", 500, { |
| 34 | + headers: { |
| 35 | + "Content-Type": "application/json", |
| 36 | + }, |
| 37 | + request: { |
| 38 | + method: "POST", |
| 39 | + url: faker.internet.url(), |
| 40 | + headers: { |
| 41 | + "Content-Type": "application/json", |
| 42 | + }, |
| 43 | + }, |
| 44 | + }); |
| 45 | + expect(handleRequestError(e, logger)).toBe(false); |
| 46 | + expect(logger.showErrorMessage).not.toHaveBeenCalled(); |
| 47 | + }); |
| 48 | + |
| 49 | + it("returns false when handling a different error with errors", () => { |
| 50 | + const e = mockRequestError(422, { |
| 51 | + message: |
| 52 | + "Unable to trigger a variant analysis. None of the requested repositories could be found.", |
| 53 | + errors: [ |
| 54 | + { |
| 55 | + resource: "VariantAnalysis", |
| 56 | + field: "repositories", |
| 57 | + code: "not_found", |
| 58 | + }, |
| 59 | + ], |
| 60 | + }); |
| 61 | + expect(handleRequestError(e, logger)).toBe(false); |
| 62 | + expect(logger.showErrorMessage).not.toHaveBeenCalled(); |
| 63 | + }); |
| 64 | + |
| 65 | + it("returns false when handling without repository field", () => { |
| 66 | + const e = mockRequestError(422, { |
| 67 | + message: |
| 68 | + "Variant analysis failed because controller repository github/pickles does not have a branch 'main'. Please create a 'main' branch in the repository and re-run the variant analysis.", |
| 69 | + errors: [ |
| 70 | + { |
| 71 | + resource: "Repository", |
| 72 | + field: "default_branch", |
| 73 | + code: "missing", |
| 74 | + default_branch: "main", |
| 75 | + }, |
| 76 | + ], |
| 77 | + }); |
| 78 | + expect(handleRequestError(e, logger)).toBe(false); |
| 79 | + expect(logger.showErrorMessage).not.toHaveBeenCalled(); |
| 80 | + }); |
| 81 | + |
| 82 | + it("returns false when handling without default_branch field", () => { |
| 83 | + const e = mockRequestError(422, { |
| 84 | + message: |
| 85 | + "Variant analysis failed because controller repository github/pickles does not have a branch 'main'. Please create a 'main' branch in the repository and re-run the variant analysis.", |
| 86 | + errors: [ |
| 87 | + { |
| 88 | + resource: "Repository", |
| 89 | + field: "default_branch", |
| 90 | + code: "missing", |
| 91 | + repository: "github/pickles", |
| 92 | + }, |
| 93 | + ], |
| 94 | + }); |
| 95 | + expect(handleRequestError(e, logger)).toBe(false); |
| 96 | + expect(logger.showErrorMessage).not.toHaveBeenCalled(); |
| 97 | + }); |
| 98 | + |
| 99 | + it("shows notification when handling a missing default branch error", () => { |
| 100 | + const e = mockRequestError(422, { |
| 101 | + message: |
| 102 | + "Variant analysis failed because controller repository github/pickles does not have a branch 'main'. Please create a 'main' branch in the repository and re-run the variant analysis.", |
| 103 | + errors: [ |
| 104 | + { |
| 105 | + resource: "Repository", |
| 106 | + field: "default_branch", |
| 107 | + code: "missing", |
| 108 | + repository: "github/pickles", |
| 109 | + default_branch: "main", |
| 110 | + }, |
| 111 | + ], |
| 112 | + }); |
| 113 | + expect(handleRequestError(e, logger)).toBe(true); |
| 114 | + expect(logger.showErrorMessage).toHaveBeenCalledWith( |
| 115 | + "Variant analysis failed because the controller repository github/pickles does not have a branch 'main'. Please create a 'main' branch by clicking [here](https://github.com/github/pickles/new/main) and re-run the variant analysis query.", |
| 116 | + ); |
| 117 | + }); |
| 118 | +}); |
| 119 | + |
| 120 | +function mockRequestError(status: number, body: any): RequestError { |
| 121 | + return new RequestError( |
| 122 | + body ? toErrorMessage(body) : "Unknown error", |
| 123 | + status, |
| 124 | + { |
| 125 | + request: { |
| 126 | + method: "POST", |
| 127 | + url: faker.internet.url(), |
| 128 | + headers: { |
| 129 | + "Content-Type": "application/json", |
| 130 | + }, |
| 131 | + }, |
| 132 | + response: { |
| 133 | + url: faker.internet.url(), |
| 134 | + status, |
| 135 | + headers: { |
| 136 | + "Content-Type": "application/json", |
| 137 | + }, |
| 138 | + data: body, |
| 139 | + }, |
| 140 | + }, |
| 141 | + ); |
| 142 | +} |
| 143 | + |
| 144 | +// Copied from https://github.com/octokit/request.js/blob/c67f902350384846f88d91196e7066daadc08357/src/fetch-wrapper.ts#L166 to have a |
| 145 | +// somewhat realistic error message |
| 146 | +function toErrorMessage(data: any) { |
| 147 | + if (typeof data === "string") return data; |
| 148 | + |
| 149 | + if ("message" in data) { |
| 150 | + if (Array.isArray(data.errors)) { |
| 151 | + return `${data.message}: ${data.errors.map(JSON.stringify).join(", ")}`; |
| 152 | + } |
| 153 | + |
| 154 | + return data.message; |
| 155 | + } |
| 156 | + |
| 157 | + // istanbul ignore next - just in case |
| 158 | + return `Unknown error: ${JSON.stringify(data)}`; |
| 159 | +} |
0 commit comments