👌 (review) update connector error handling
Improve error handling by creating a function handler for connections on servers that use KY, and use the status key in the response as the key to success instead of catch. Use catch if an error occurs between the client and server, not between the server and backend.
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
"use server";
|
||||
import { api } from "@/shared/api/connector";
|
||||
import { api } from "@/shared/lib/ky/connector";
|
||||
|
||||
const getOauthProviderList = async () => {
|
||||
try {
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
"use server";
|
||||
|
||||
import { api } from "@/shared/api/connector";
|
||||
import { api } from "@/shared/lib/ky/connector";
|
||||
import { redirect } from "next/navigation";
|
||||
import { ResponseRequestOauthUrl } from "../types/responseRequestOauthUrl";
|
||||
|
||||
|
||||
@ -1,5 +1,14 @@
|
||||
"use server";
|
||||
import { api } from "@/shared/api/connector";
|
||||
import { api } from "@/shared/lib/ky/connector";
|
||||
import { apiErrorHandler } from "@/shared/lib/ky/errorHandler";
|
||||
import { HTTPError } from "ky";
|
||||
|
||||
interface SendCallbackResponse {
|
||||
success: boolean;
|
||||
status: number;
|
||||
text: { message: string };
|
||||
data?: any;
|
||||
}
|
||||
|
||||
/**
|
||||
* @function SendCallbackToServer
|
||||
@ -52,7 +61,10 @@ import { api } from "@/shared/api/connector";
|
||||
* - The callback URI is automatically constructed using the APP_DOMAIN environment variable.
|
||||
* - Ensure APP_DOMAIN is properly configured in your environment variables.
|
||||
*/
|
||||
export const SendCallbackToServer = async (data: string, provider: string) => {
|
||||
export const SendCallbackToServer = async (
|
||||
data: string,
|
||||
provider: string
|
||||
): Promise<SendCallbackResponse> => {
|
||||
// Construct the backend and frontend handler URLs
|
||||
const backendHandlerUrl = `auth/${provider}/callback/`;
|
||||
const frontendHandlerUrl = `${process.env
|
||||
@ -66,8 +78,13 @@ export const SendCallbackToServer = async (data: string, provider: string) => {
|
||||
|
||||
// Parse the JSON response from the backend and return the result
|
||||
const result = await response.json();
|
||||
return result;
|
||||
return {
|
||||
success: true,
|
||||
status: response.status,
|
||||
text: { message: "Callback processed successfully" },
|
||||
data: result,
|
||||
};
|
||||
} catch (error) {
|
||||
throw error;
|
||||
return apiErrorHandler(error);
|
||||
}
|
||||
};
|
||||
|
||||
@ -14,15 +14,34 @@ const LoadingProcess = () => {
|
||||
// Forward the callback response to the backend server
|
||||
useRunOnce("forwardCallbackResponseToBackend", async () => {
|
||||
try {
|
||||
await SendCallbackToServer(
|
||||
const response = await SendCallbackToServer(
|
||||
window.location.search,
|
||||
params.provider as string
|
||||
);
|
||||
window.close();
|
||||
|
||||
if (response.success) {
|
||||
window.close();
|
||||
} else {
|
||||
addToast({
|
||||
title: "😬 Oops, there's a problem!",
|
||||
description: response.text.message,
|
||||
color: "danger",
|
||||
timeout: 0,
|
||||
endContent: (
|
||||
<Button
|
||||
size="sm"
|
||||
variant="flat"
|
||||
onPress={() => (window.location.href = routes.login)}
|
||||
>
|
||||
Try again
|
||||
</Button>
|
||||
),
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
addToast({
|
||||
title: "Oops, lost connection!",
|
||||
title: "😵💫 Oops, lost connection!",
|
||||
description: "Check your internet and try again",
|
||||
color: "danger",
|
||||
timeout: 0,
|
||||
|
||||
26
shared/lib/ky/errorHandler.ts
Normal file
26
shared/lib/ky/errorHandler.ts
Normal file
@ -0,0 +1,26 @@
|
||||
"use server";
|
||||
|
||||
import { HTTPError } from "ky";
|
||||
|
||||
export const apiErrorHandler = async (
|
||||
error: unknown,
|
||||
safeFail: boolean = false
|
||||
) => {
|
||||
if (error instanceof HTTPError) {
|
||||
return {
|
||||
success: false,
|
||||
status: error.response.status,
|
||||
text: await error.response.json(),
|
||||
};
|
||||
}
|
||||
|
||||
if (safeFail) {
|
||||
return {
|
||||
success: false,
|
||||
status: 500,
|
||||
text: { message: "An unexpected error occurred" },
|
||||
};
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user