Bläddra i källkod

获取购物车数据接口错误处理

fogwind 1 vecka sedan
förälder
incheckning
2f4e8f5089
2 ändrade filer med 17 tillägg och 5 borttagningar
  1. 2 1
      src/app/api/graphql/route.ts
  2. 15 4
      src/utils/bagisto/index.ts

+ 2 - 1
src/app/api/graphql/route.ts

@@ -107,8 +107,9 @@ export async function POST(req: NextRequest) {
             variables: finalVariables,
             cache: "no-store",
             guestToken,
+            operationName
         });
-
+        // console.log('response ------ ', response);
         return NextResponse.json({
             data: response.body.data,
         });

+ 15 - 4
src/utils/bagisto/index.ts

@@ -141,6 +141,7 @@ export async function bagistoFetch<T>({
   isCookies = true,
   guestToken,
   revalidate = 60,
+  operationName = ''
 }: {
   cache?: RequestCache;
   headers?: HeadersInit | Record<string, string>;
@@ -150,6 +151,7 @@ export async function bagistoFetch<T>({
   isCookies?: boolean;
   guestToken?: string;
   revalidate?: number;
+  operationName?: string;
 }): Promise<{ status: number; body: T } | never> {
   try {
     const queryString =
@@ -189,7 +191,12 @@ export async function bagistoFetch<T>({
         Object.assign(baseHeaders, headers);
       }
     }
-
+    // let cc = await cookies();
+    // console.log('bagistoFetch --- url:', GRAPHQL_URL,isCookies);
+    // console.log('bagistoFetch --- queryString:', queryString);
+    // console.log('bagistoFetch --- baseHeaders:', baseHeaders);
+    // console.log('bagistoFetch --- cookies:', cc.getAll());
+    // console.log('bagistoFetch --- variables:', variables);
     const result = await fetch(GRAPHQL_URL, {
       method: "POST",
       headers: baseHeaders,
@@ -205,9 +212,13 @@ export async function bagistoFetch<T>({
     });
 
     const body = await result.json();
-
-    if (body.errors) throw body.errors[0];
-
+    // console.log('bagistoFetch --- body:',body);
+    if (body.errors) {
+      if(operationName === 'GetCartItem' && body.errors[0].message === 'Cart not found') {
+        return { status: result.status, body };
+      }
+      throw body.errors[0]
+    }
     return { status: result.status, body };
   } catch (e) {
     throw e;