Skip to content

Commit

Permalink
Merge pull request #145 from argos-ci/improve-error-handling
Browse files Browse the repository at this point in the history
feat: improve error handling
  • Loading branch information
gregberge committed Aug 31, 2024
2 parents fd9935c + baed0ef commit 37d76cc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
26 changes: 25 additions & 1 deletion packages/api-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import createFetchClient from "openapi-fetch";
import createFetchClient, { FetchResponse } from "openapi-fetch";
import type { paths } from "./schema";

export type ArgosAPIClient = ReturnType<typeof createClient>;
Expand All @@ -15,3 +15,27 @@ export function createClient(options: { baseUrl?: string; authToken: string }) {
},
});
}

export class APIError extends Error {
constructor(message: string) {
super(message);
}
}

/**
* 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}`);
}
4 changes: 2 additions & 2 deletions packages/core/src/finalize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createClient } from "@argos-ci/api-client";
import { createClient, throwAPIError } from "@argos-ci/api-client";
import { getAuthToken } from "./auth";
import { readConfig } from "./config";

Expand Down Expand Up @@ -33,7 +33,7 @@ export async function finalize(params: FinalizeParameters) {
});

if (finalizeBuildsResult.error) {
throw new Error(finalizeBuildsResult.error.error);
throwAPIError(finalizeBuildsResult);
}

return finalizeBuildsResult.data;
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/upload.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createClient } from "@argos-ci/api-client";
import { createClient, throwAPIError } from "@argos-ci/api-client";
import { readConfig } from "./config";
import { discoverScreenshots } from "./discovery";
import { optimizeScreenshot } from "./optimize";
Expand Down Expand Up @@ -201,7 +201,7 @@ export async function upload(params: UploadParameters) {
debug("Fetch project");
const projectResponse = await apiClient.GET("/project");
if (projectResponse.error) {
throw new Error(projectResponse.error.error);
throwAPIError(projectResponse);
}
const { defaultBaseBranch, hasRemoteContentAccess } = projectResponse.data;
const referenceBranch = config.referenceBranch || defaultBaseBranch;
Expand Down Expand Up @@ -267,7 +267,7 @@ export async function upload(params: UploadParameters) {
});

if (createBuildResponse.error) {
throw new Error(createBuildResponse.error.error);
throwAPIError(createBuildResponse);
}

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

if (uploadBuildResponse.error) {
throw new Error(uploadBuildResponse.error.error);
throwAPIError(uploadBuildResponse);
}

return { build: uploadBuildResponse.data.build, screenshots };
Expand Down

0 comments on commit 37d76cc

Please sign in to comment.