Skip to content

Commit

Permalink
Fix error mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
lkostrowski committed May 17, 2024
1 parent 59d6aa3 commit 24ef571
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { createLogger } from "../../../logger";
export const throwTrpcErrorFromConfigurationServiceError = (
error: typeof SmtpConfigurationService.SmtpConfigurationServiceError | unknown,
) => {
createLogger("trpcError").debug("Error from TRPC", { error });

if (error instanceof SmtpConfigurationService.SmtpConfigurationServiceError) {
switch (error["constructor"]) {
case SmtpConfigurationService.ConfigNotFoundError:
Expand Down
36 changes: 24 additions & 12 deletions apps/smtp/src/modules/smtp/configuration/smtp-metadata-manager.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { SettingsManager } from "@saleor/app-sdk/settings-manager";
import { SmtpConfig } from "./smtp-config-schema";
import { fromAsyncThrowable, fromThrowable, ok, ResultAsync } from "neverthrow";
import { fromAsyncThrowable, fromPromise, fromThrowable, ok, ResultAsync } from "neverthrow";
import { BaseError } from "../../../errors";
import { createLogger } from "../../../logger";

// todo test
export class SmtpMetadataManager {
private metadataKey = "smtp-config";
private logger = createLogger("SmtpMetadataManager");

static SmtpMetadataManagerError = BaseError.subclass("SmtpMetadataManagerError");
static ParseConfigError = this.SmtpMetadataManagerError.subclass("ParseConfigError");
Expand All @@ -22,28 +25,37 @@ export class SmtpMetadataManager {
typeof SmtpMetadataManager.ParseConfigError | typeof SmtpMetadataManager.FetchConfigError
>
> {
return fromAsyncThrowable(
this.metadataManager.get,
SmtpMetadataManager.FetchConfigError.normalize,
)(this.metadataKey, this.saleorApiUrl).andThen((config) => {
this.logger.debug("Fetching SMTP config");

return fromPromise(this.metadataManager.get(this.metadataKey, this.saleorApiUrl), (e) => {
this.logger.debug("Failed to fetch config", { error: e });

return new SmtpMetadataManager.FetchConfigError("Failed to fetch metadata", { errors: [e] });
}).andThen((config) => {
if (!config) {
this.logger.debug("Config not found, returning undefined");

return ok(undefined);
}

this.logger.debug("Config found, will parse JSON now");

return fromThrowable(JSON.parse, SmtpMetadataManager.ParseConfigError.normalize)(config);
});
}

setConfig(
config: SmtpConfig,
): ResultAsync<void, InstanceType<typeof SmtpMetadataManager.SetConfigError>> {
return ResultAsync.fromThrowable(
this.metadataManager.set,
this.logger.debug("Trying to set config");

return ResultAsync.fromPromise(
this.metadataManager.set({
key: this.metadataKey,
value: JSON.stringify(config),
domain: this.saleorApiUrl,
}),
SmtpMetadataManager.SetConfigError.normalize,
)({
key: this.metadataKey,
value: JSON.stringify(config),
domain: this.saleorApiUrl,
});
);
}
}
2 changes: 1 addition & 1 deletion apps/smtp/src/modules/smtp/services/smtp-email-sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class SmtpEmailSender implements ISMTPEmailSender {
...mailData,
});

this.logger.debug("An email has been sent");
this.logger.debug("An email has been sent", { response });

return { response };
} catch (error) {
Expand Down
41 changes: 19 additions & 22 deletions apps/smtp/src/pages/api/webhooks/gift-card-sent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,29 +120,26 @@ const handler: NextWebhookApiHandler<GiftCardSentWebhookPayloadFragment> = async
return res.status(200).json({ message: "The event has been handled" });
},
(err) => {
switch (err[0].constructor) {
case SendEventMessagesUseCase.ServerError: {
logger.error("Failed to send email(s) [server error]", { error: err });

return res.status(500).json({ message: "Failed to send email" });
}
case SendEventMessagesUseCase.ClientError: {
logger.info("Failed to send email(s) [client error]", { error: err });

return res.status(400).json({ message: "Failed to send email" });
}
case SendEventMessagesUseCase.NoOpError: {
logger.error("Sending emails aborted [no op]", { error: err });

return res.status(200).json({ message: "The event has been handled [no op]" });
}
default: {
logger.error("Failed to send email(s) [server error]", { error: err });
captureException(new Error("Unhandled useCase error", { cause: err }));

return res.status(500).json({ message: "Failed to send email [unhandled]" });
}
const errorInstance = err[0];

if (errorInstance instanceof SendEventMessagesUseCase.ServerError) {
logger.error("Failed to send email(s) [server error]", { error: err });

return res.status(500).json({ message: "Failed to send email" });
} else if (errorInstance instanceof SendEventMessagesUseCase.ClientError) {
logger.info("Failed to send email(s) [client error]", { error: err });

return res.status(400).json({ message: "Failed to send email" });
} else if (errorInstance instanceof SendEventMessagesUseCase.NoOpError) {
logger.error("Sending emails aborted [no op]", { error: err });

return res.status(200).json({ message: "The event has been handled [no op]" });
}

logger.error("Failed to send email(s) [unhandled error]", { error: err });
captureException(new Error("Unhandled useCase error", { cause: err }));

return res.status(500).json({ message: "Failed to send email [unhandled]" });
},
),
);
Expand Down
41 changes: 19 additions & 22 deletions apps/smtp/src/pages/api/webhooks/invoice-sent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,26 @@ const handler: NextWebhookApiHandler<InvoiceSentWebhookPayloadFragment> = async
return res.status(200).json({ message: "The event has been handled" });
},
(err) => {
switch (err[0].constructor) {
case SendEventMessagesUseCase.ServerError: {
logger.error("Failed to send email(s) [server error]", { error: err });

return res.status(500).json({ message: "Failed to send email" });
}
case SendEventMessagesUseCase.ClientError: {
logger.info("Failed to send email(s) [client error]", { error: err });

return res.status(400).json({ message: "Failed to send email" });
}
case SendEventMessagesUseCase.NoOpError: {
logger.error("Sending emails aborted [no op]", { error: err });

return res.status(200).json({ message: "The event has been handled [no op]" });
}
default: {
logger.error("Failed to send email(s) [server error]", { error: err });
captureException(new Error("Unhandled useCase error", { cause: err }));

return res.status(500).json({ message: "Failed to send email [unhandled]" });
}
const errorInstance = err[0];

if (errorInstance instanceof SendEventMessagesUseCase.ServerError) {
logger.error("Failed to send email(s) [server error]", { error: err });

return res.status(500).json({ message: "Failed to send email" });
} else if (errorInstance instanceof SendEventMessagesUseCase.ClientError) {
logger.info("Failed to send email(s) [client error]", { error: err });

return res.status(400).json({ message: "Failed to send email" });
} else if (errorInstance instanceof SendEventMessagesUseCase.NoOpError) {
logger.error("Sending emails aborted [no op]", { error: err });

return res.status(200).json({ message: "The event has been handled [no op]" });
}

logger.error("Failed to send email(s) [unhandled error]", { error: err });
captureException(new Error("Unhandled useCase error", { cause: err }));

return res.status(500).json({ message: "Failed to send email [unhandled]" });
},
),
);
Expand Down
41 changes: 19 additions & 22 deletions apps/smtp/src/pages/api/webhooks/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,26 @@ const handler: NextWebhookApiHandler<NotifySubscriptionPayload> = async (req, re
return res.status(200).json({ message: "The event has been handled" });
},
(err) => {
switch (err[0].constructor) {
case SendEventMessagesUseCase.ServerError: {
logger.error("Failed to send email(s) [server error]", { error: err });

return res.status(500).json({ message: "Failed to send email" });
}
case SendEventMessagesUseCase.ClientError: {
logger.info("Failed to send email(s) [client error]", { error: err });

return res.status(400).json({ message: "Failed to send email" });
}
case SendEventMessagesUseCase.NoOpError: {
logger.error("Sending emails aborted [no op]", { error: err });

return res.status(200).json({ message: "The event has been handled [no op]" });
}
default: {
logger.error("Failed to send email(s) [server error]", { error: err });
captureException(new Error("Unhandled useCase error", { cause: err }));

return res.status(500).json({ message: "Failed to send email [unhandled]" });
}
const errorInstance = err[0];

if (errorInstance instanceof SendEventMessagesUseCase.ServerError) {
logger.error("Failed to send email(s) [server error]", { error: err });

return res.status(500).json({ message: "Failed to send email" });
} else if (errorInstance instanceof SendEventMessagesUseCase.ClientError) {
logger.info("Failed to send email(s) [client error]", { error: err });

return res.status(400).json({ message: "Failed to send email" });
} else if (errorInstance instanceof SendEventMessagesUseCase.NoOpError) {
logger.error("Sending emails aborted [no op]", { error: err });

return res.status(200).json({ message: "The event has been handled [no op]" });
}

logger.error("Failed to send email(s) [unhandled error]", { error: err });
captureException(new Error("Unhandled useCase error", { cause: err }));

return res.status(500).json({ message: "Failed to send email [unhandled]" });
},
),
);
Expand Down
41 changes: 19 additions & 22 deletions apps/smtp/src/pages/api/webhooks/order-cancelled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,29 +84,26 @@ const handler: NextWebhookApiHandler<OrderCancelledWebhookPayloadFragment> = asy
return res.status(200).json({ message: "The event has been handled" });
},
(err) => {
switch (err[0].constructor) {
case SendEventMessagesUseCase.ServerError: {
logger.error("Failed to send email(s) [server error]", { error: err });

return res.status(500).json({ message: "Failed to send email" });
}
case SendEventMessagesUseCase.ClientError: {
logger.info("Failed to send email(s) [client error]", { error: err });

return res.status(400).json({ message: "Failed to send email" });
}
case SendEventMessagesUseCase.NoOpError: {
logger.error("Sending emails aborted [no op]", { error: err });

return res.status(200).json({ message: "The event has been handled [no op]" });
}
default: {
logger.error("Failed to send email(s) [server error]", { error: err });
captureException(new Error("Unhandled useCase error", { cause: err }));

return res.status(500).json({ message: "Failed to send email [unhandled]" });
}
const errorInstance = err[0];

if (errorInstance instanceof SendEventMessagesUseCase.ServerError) {
logger.error("Failed to send email(s) [server error]", { error: err });

return res.status(500).json({ message: "Failed to send email" });
} else if (errorInstance instanceof SendEventMessagesUseCase.ClientError) {
logger.info("Failed to send email(s) [client error]", { error: err });

return res.status(400).json({ message: "Failed to send email" });
} else if (errorInstance instanceof SendEventMessagesUseCase.NoOpError) {
logger.error("Sending emails aborted [no op]", { error: err });

return res.status(200).json({ message: "The event has been handled [no op]" });
}

logger.error("Failed to send email(s) [unhandled error]", { error: err });
captureException(new Error("Unhandled useCase error", { cause: err }));

return res.status(500).json({ message: "Failed to send email [unhandled]" });
},
),
);
Expand Down
41 changes: 19 additions & 22 deletions apps/smtp/src/pages/api/webhooks/order-confirmed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,29 +85,26 @@ const handler: NextWebhookApiHandler<OrderConfirmedWebhookPayloadFragment> = asy
return res.status(200).json({ message: "The event has been handled" });
},
(err) => {
switch (err[0].constructor) {
case SendEventMessagesUseCase.ServerError: {
logger.error("Failed to send email(s) [server error]", { error: err });

return res.status(500).json({ message: "Failed to send email" });
}
case SendEventMessagesUseCase.ClientError: {
logger.info("Failed to send email(s) [client error]", { error: err });

return res.status(400).json({ message: "Failed to send email" });
}
case SendEventMessagesUseCase.NoOpError: {
logger.error("Sending emails aborted [no op]", { error: err });

return res.status(200).json({ message: "The event has been handled [no op]" });
}
default: {
logger.error("Failed to send email(s) [server error]", { error: err });
captureException(new Error("Unhandled useCase error", { cause: err }));

return res.status(500).json({ message: "Failed to send email [unhandled]" });
}
const errorInstance = err[0];

if (errorInstance instanceof SendEventMessagesUseCase.ServerError) {
logger.error("Failed to send email(s) [server error]", { error: err });

return res.status(500).json({ message: "Failed to send email" });
} else if (errorInstance instanceof SendEventMessagesUseCase.ClientError) {
logger.info("Failed to send email(s) [client error]", { error: err });

return res.status(400).json({ message: "Failed to send email" });
} else if (errorInstance instanceof SendEventMessagesUseCase.NoOpError) {
logger.error("Sending emails aborted [no op]", { error: err });

return res.status(200).json({ message: "The event has been handled [no op]" });
}

logger.error("Failed to send email(s) [unhandled error]", { error: err });
captureException(new Error("Unhandled useCase error", { cause: err }));

return res.status(500).json({ message: "Failed to send email [unhandled]" });
},
),
);
Expand Down
41 changes: 19 additions & 22 deletions apps/smtp/src/pages/api/webhooks/order-created.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,29 +82,26 @@ const handler: NextWebhookApiHandler<OrderCreatedWebhookPayloadFragment> = async
return res.status(200).json({ message: "The event has been handled" });
},
(err) => {
switch (err[0].constructor) {
case SendEventMessagesUseCase.ServerError: {
logger.error("Failed to send email(s) [server error]", { error: err });

return res.status(500).json({ message: "Failed to send email" });
}
case SendEventMessagesUseCase.ClientError: {
logger.info("Failed to send email(s) [client error]", { error: err });

return res.status(400).json({ message: "Failed to send email" });
}
case SendEventMessagesUseCase.NoOpError: {
logger.error("Sending emails aborted [no op]", { error: err });

return res.status(200).json({ message: "The event has been handled [no op]" });
}
default: {
logger.error("Failed to send email(s) [server error]", { error: err });
captureException(new Error("Unhandled useCase error", { cause: err }));

return res.status(500).json({ message: "Failed to send email [unhandled]" });
}
const errorInstance = err[0];

if (errorInstance instanceof SendEventMessagesUseCase.ServerError) {
logger.error("Failed to send email(s) [server error]", { error: err });

return res.status(500).json({ message: "Failed to send email" });
} else if (errorInstance instanceof SendEventMessagesUseCase.ClientError) {
logger.info("Failed to send email(s) [client error]", { error: err });

return res.status(400).json({ message: "Failed to send email" });
} else if (errorInstance instanceof SendEventMessagesUseCase.NoOpError) {
logger.error("Sending emails aborted [no op]", { error: err });

return res.status(200).json({ message: "The event has been handled [no op]" });
}

logger.error("Failed to send email(s) [unhandled error]", { error: err });
captureException(new Error("Unhandled useCase error", { cause: err }));

return res.status(500).json({ message: "Failed to send email [unhandled]" });
},
),
);
Expand Down
Loading

0 comments on commit 24ef571

Please sign in to comment.