👌 (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:
2025-09-13 12:50:43 +07:00
parent 3e6eae5151
commit 6d5fee823e
8 changed files with 71 additions and 9 deletions

View File

@ -1,5 +1,5 @@
"use server";
import { api } from "@/shared/api/connector";
import { api } from "@/shared/lib/ky/connector";
const getOauthProviderList = async () => {
try {

View File

@ -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";

View File

@ -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);
}
};

View File

@ -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,