Skip to content

Commit

Permalink
handle document not found error in avatax during cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
lkostrowski committed Sep 13, 2024
1 parent badd1f5 commit d11999d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/shiny-wombats-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"app-avatax": patch
---

Add handling for error occurred due to missing document in AvaTax during cancel (voiding)
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { z } from "zod";

import { BaseError } from "@/error";

import { createLogger } from "../../../logger";
import { CancelOrderPayload } from "../../taxes/tax-provider-webhook";
import { WebhookAdapter } from "../../taxes/tax-webhook-adapter";
Expand All @@ -11,11 +15,28 @@ export type AvataxOrderCancelledTarget = VoidTransactionArgs;
export class AvataxOrderCancelledAdapter implements WebhookAdapter<{ avataxId: string }, void> {
private logger = createLogger("AvataxOrderCancelledAdapter");

static AvaTaxOrderCancelledAdapterError = BaseError.subclass("AvaTaxOrderCancelledAdapterError");
static DocumentNotFoundError =
this.AvaTaxOrderCancelledAdapterError.subclass("DocumentNotFoundError");

constructor(
private avataxClient: AvataxClient,
private avataxOrderCancelledPayloadTransformer: AvataxOrderCancelledPayloadTransformer,
) {}

/**
* Locally extract error code - but this should be more global, with some normalization/parsing logic/middleware
*/
private extractAvaTaxErrorCode = (error: unknown): string | null => {
const parsedError = z.object({ code: z.string() }).safeParse(error);

if (!parsedError.success) {
return null;
}

return parsedError.data.code;
};

async send(payload: CancelOrderPayload, config: AvataxConfig) {
this.logger.info("Transforming the Saleor payload for cancelling transaction with AvaTax...");

Expand All @@ -42,6 +63,26 @@ export class AvataxOrderCancelledAdapter implements WebhookAdapter<{ avataxId: s
avataxId: payload.avataxId,
});
} catch (e) {
const code = this.extractAvaTaxErrorCode(e);

/**
* This can happen when AvaTax doesn't have document on their side.
* We can't do anything about - hence custom handling of this error
*/
if (code === "EntityNotFoundError") {
/**
* TODO Replace with neverthrow one day
*/
throw new AvataxOrderCancelledAdapter.DocumentNotFoundError(
"AvaTax didnt find the document to void",
{
props: {
error: e,
},
},
);
}

const error = normalizeAvaTaxError(e);

this.logger.error("Error voiding the transaction", {
Expand Down
24 changes: 18 additions & 6 deletions apps/avatax/src/pages/api/webhooks/order-cancelled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { ObservabilityAttributes } from "@saleor/apps-otel/src/lib/observability
import * as Sentry from "@sentry/nextjs";
import { captureException } from "@sentry/nextjs";

import { AvataxOrderCancelledAdapter } from "@/modules/avatax/order-cancelled/avatax-order-cancelled-adapter";

import { AppConfigExtractor } from "../../../lib/app-config-extractor";
import { AppConfigurationLogger } from "../../../lib/app-configuration-logger";
import { metadataCache, wrapWithMetadataCache } from "../../../lib/app-metadata-cache";
Expand Down Expand Up @@ -139,12 +141,22 @@ export default wrapWithLoggerContext(
.json({ message: `App is not configured properly for order: ${payload.order?.id}` });
}

await taxProvider.cancelOrder(
{
avataxId: cancelledOrderInstance.getAvataxId(),
},
providerConfig.value.avataxConfig.config,
);
try {
await taxProvider.cancelOrder(
{
avataxId: cancelledOrderInstance.getAvataxId(),
},
providerConfig.value.avataxConfig.config,
);
} catch (e) {
if (e instanceof AvataxOrderCancelledAdapter.DocumentNotFoundError) {
logger.warn("Document was not found in AvaTax. Responding 400", {
error: e,
});

return res.status(400).end();
}
}

logger.info("Order cancelled");

Expand Down

0 comments on commit d11999d

Please sign in to comment.