Add more debug messages to SaleorCloudAPL (#175)

* Add more debug messages to SaleorCloudAPL

* Refactor error message extraction
This commit is contained in:
Lukasz Ostrowski 2023-02-14 13:00:46 +01:00 committed by GitHub
parent 24778df592
commit a15d72602f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,3 +1,4 @@
import { hasProp } from "../has-prop";
import { APL, AplConfiguredResult, AplReadyResult, AuthData } from "./apl";
import { createAPLDebug } from "./apl-debug";
import { authDataFromObject } from "./auth-data-from-object";
@ -12,11 +13,13 @@ export type SaleorCloudAPLConfig = {
const validateResponseStatus = (response: Response) => {
if (response.status === 404) {
debug("Auth data not found");
debug("%O", response);
throw new Error("Auth data not found");
}
if (!response.ok) {
debug("Response failed with status %s", response.status);
debug("%O", response);
throw new Error(`Fetch returned with non 200 status code ${response.status}`);
}
@ -39,6 +42,18 @@ const mapAPIResponseToAuthData = (response: any): AuthData => ({
token: response.token,
});
const extractErrorMessage = (error: unknown) => {
if (typeof error === "string") {
return error;
}
if (hasProp(error, "message")) {
return error.message;
}
return "Unknown error";
};
/**
*
* Saleor Cloud APL - handle auth data management via REST API.
@ -72,7 +87,9 @@ export class SaleorCloudAPL implements APL {
method: "GET",
headers: { "Content-Type": "application/json", ...this.headers },
}).catch((error) => {
debug("Failed to reach API call: %s", error?.message ?? "Unknown error");
debug("Failed to reach API call: %s", extractErrorMessage(error));
debug("%O", error);
return undefined;
});
@ -83,13 +100,15 @@ export class SaleorCloudAPL implements APL {
try {
validateResponseStatus(response);
debug("Response status valid");
} catch {
debug("Response status not valid");
return undefined;
}
const parsedResponse = (await response.json().catch((e) => {
debug("Failed to parse response: %s", e?.message ?? "Unknown error");
debug("Failed to parse response: %s", extractErrorMessage(e));
debug("%O", e);
})) as unknown;
const authData = authDataFromObject(mapAPIResponseToAuthData(parsedResponse));
@ -103,21 +122,22 @@ export class SaleorCloudAPL implements APL {
}
async set(authData: AuthData) {
debug("Saving data to SaleorCloudAPL for domain: %s", authData.domain);
debug("Saving data to SaleorCloudAPL for saleorApiUrl: %s", authData.saleorApiUrl);
const response = await fetch(this.resourceUrl, {
method: "POST",
headers: { "Content-Type": "application/json", ...this.headers },
body: JSON.stringify(mapAuthDataToAPIBody(authData)),
}).catch((e) => {
debug("Failed to reach API call: %s", e?.message ?? "Unknown error");
debug("Failed to reach API call: %s", extractErrorMessage(e));
debug("%O", e);
throw new Error(`Error during saving the data: ${e}`);
throw new Error(`Error during saving the data: ${extractErrorMessage(e)}`);
});
validateResponseStatus(response);
debug("Set command finished successfully for domain: %", authData.domain);
debug("Set command finished successfully for saleorApiUrl: %", authData.saleorApiUrl);
return undefined;
}
@ -133,9 +153,12 @@ export class SaleorCloudAPL implements APL {
debug(`Delete responded with ${response.status} code`);
} catch (error) {
debug("Error during deleting the data: %s", error);
const errorMessage = extractErrorMessage(error);
throw new Error(`Error during deleting the data: ${error}`);
debug("Error during deleting the data: %s", errorMessage);
debug("%O", error);
throw new Error(`Error during deleting the data: ${errorMessage}`);
}
}
@ -152,7 +175,10 @@ export class SaleorCloudAPL implements APL {
return ((await response.json()) as AuthData[]) || [];
} catch (error) {
debug("Error during getting all the data:", error);
const errorMessage = extractErrorMessage(error);
debug("Error during getting all the data:", errorMessage);
debug("%O", error);
}
return [];