Skip to content

Commit

Permalink
fix: display error messages from API (#152)
Browse files Browse the repository at this point in the history
Also add a new debug scope for that.
This makes debugging easier for customers.
  • Loading branch information
sidharthv96 authored Oct 8, 2024
1 parent c109fcd commit 44def81
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 92 deletions.
2 changes: 2 additions & 0 deletions packages/api-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@
"build": "rollup -c"
},
"devDependencies": {
"@types/debug": "^4.1.12",
"@types/node": "^18.19.44",
"openapi-typescript": "^7.3.0"
},
"dependencies": {
"debug": "^4.3.6",
"openapi-fetch": "^0.11.1"
}
}
5 changes: 5 additions & 0 deletions packages/api-client/src/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import createDebug from "debug";

const KEY = "@argos-ci/api-client";

export const debug = createDebug(KEY);
27 changes: 13 additions & 14 deletions packages/api-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import createFetchClient, { FetchResponse } from "openapi-fetch";
import createFetchClient from "openapi-fetch";
import { debug } from "./debug";
import type { paths } from "./schema";

export * as ArgosAPISchema from "./schema";
Expand Down Expand Up @@ -27,17 +28,15 @@ export class APIError extends Error {
/**
* Handle API errors.
*/
export function throwAPIError(
fetchResponse: FetchResponse<any, any, any>,
): never {
const { error, response } = fetchResponse;
if (
error &&
typeof error === "object" &&
"error" in error &&
typeof error.error === "string"
) {
throw new APIError(error.error);
}
throw new APIError(`API error: ${response.status} ${response.statusText}`);
export function throwAPIError(error: {
error: string;
details: {
message: string;
}[];
}): never {
debug("API error", error);
const detailMessage = error.details
.map((detail) => detail.message)
.join(", ");
throw new APIError(`${error.error}: ${detailMessage}`);
}
2 changes: 1 addition & 1 deletion packages/core/src/finalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function finalize(params: FinalizeParameters) {
});

if (finalizeBuildsResult.error) {
throwAPIError(finalizeBuildsResult);
throwAPIError(finalizeBuildsResult.error);
}

return finalizeBuildsResult.data;
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export async function upload(params: UploadParameters) {
debug("Fetch project");
const projectResponse = await apiClient.GET("/project");
if (projectResponse.error) {
throwAPIError(projectResponse);
throwAPIError(projectResponse.error);
}
const { defaultBaseBranch, hasRemoteContentAccess } = projectResponse.data;
const referenceBranch = config.referenceBranch || defaultBaseBranch;
Expand Down Expand Up @@ -277,7 +277,7 @@ export async function upload(params: UploadParameters) {
});

if (createBuildResponse.error) {
throwAPIError(createBuildResponse);
throwAPIError(createBuildResponse.error);
}

const result = createBuildResponse.data;
Expand Down Expand Up @@ -339,7 +339,7 @@ export async function upload(params: UploadParameters) {
});

if (uploadBuildResponse.error) {
throwAPIError(uploadBuildResponse);
throwAPIError(uploadBuildResponse.error);
}

return { build: uploadBuildResponse.data.build, screenshots };
Expand Down
2 changes: 1 addition & 1 deletion packages/webdriverio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@wdio/mocha-framework": "^9.0.0",
"@wdio/spec-reporter": "^9.0.0",
"@wdio/types": "^9.0.0",
"chromedriver": "^127.0.3",
"chromedriver": "^129.0.2",
"tsx": "^4.17.0",
"wdio-chromedriver-service": "^8.1.1",
"webdriverio": "^9.0.1"
Expand Down
Loading

0 comments on commit 44def81

Please sign in to comment.