Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isClerkAPIResponseError is not a function #4591

Open
4 tasks done
lmyslinski opened this issue Nov 17, 2024 · 2 comments
Open
4 tasks done

isClerkAPIResponseError is not a function #4591

lmyslinski opened this issue Nov 17, 2024 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@lmyslinski
Copy link

lmyslinski commented Nov 17, 2024

Preliminary Checks

Reproduction

Will create if needed

Publishable key

Will add if needed

Description

I'm upgrading to v6 and cannot use the provided error handlers:

⨯ TypeError: (0 , clerk_nextjs_errors__WEBPACK_IMPORTED_MODULE_4_.isClerkAPIResponseError) is not a function

Error handler:
...
import {
    isClerkAPIResponseError,
} from '@clerk/nextjs/errors';
...

export function withErrorHandler(handler: Handler): Handler {
    return async (req: NextRequest, ...args: any[]) => {
        try {
            return await handler(req, ...args);
        } catch (error: unknown) {
           ...
            if (isClerkAPIResponseError(error)) {
                return NextResponse.json(
                    new ApiError(
                        error.message,
                        500,
                        ApiErrorCode.INTERNAL_SERVER_ERROR
                    ).toJSON(),
                    { status: 500 }
                );
            }
...
        }
    };
}

Route that throws the error:

async function deleteHandler(request: NextRequest): Promise<NextResponse<OrganizationLeaveResponse>> {
   ... 
    await client.organizations.deleteOrganizationMembership({
        userId: userId,
        organizationId: orgId,
    });
   ...
}

export const DELETE = withErrorHandler(deleteHandler);

Actual error from logs:

[07:09:06.220] ERROR (api-error-handler/26689): API Error occurred
    error: {
      "status": 400,
      "clerkTraceId": "0128e81e3b5242bd15a9da2e54cf54eb",
      "clerkError": true,
      "errors": [
        {
          "code": "organization_minimum_permissions_needed",
          "message": "minimum organization permissions needed",
          "longMessage": "There has to be at least one organization member with the minimum required permissions",
          "meta": {}
        }
      ]
    }

Instead of isClerkAPIResponseError executing, I get an import failure. Full stack trace:

 ⨯ TypeError: (0 , _clerk_nextjs_errors__WEBPACK_IMPORTED_MODULE_4__.isClerkAPIResponseError) is not a function
    at eval (webpack-internal:///(rsc)/./app/api/api-error-handler.tsx:48:94)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async eval (webpack-internal:///(rsc)/./node_modules/.pnpm/@sentry+nextjs@8.38.0_@opentelemetry+core@1.27.0_@opentelemetry+api@1.9.0__@opentelemetry+ins_pukndwabmym6vqunvov2jrcvsm/node_modules/@sentry/nextjs/build/cjs/common/wrapRouteHandlerWithSentry.js:40:30)
    at async /home/lmyslinski/Dev/cv2b/cv2b/web/node_modules/.pnpm/next@14.2.18_@babel+core@7.26.0_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:55831
    at async eO.execute (/home/lmyslinski/Dev/cv2b/cv2b/web/node_modules/.pnpm/next@14.2.18_@babel+core@7.26.0_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:46527)
    at async eO.handle (/home/lmyslinski/Dev/cv2b/cv2b/web/node_modules/.pnpm/next@14.2.18_@babel+core@7.26.0_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:57165)
    at async doRender (/home/lmyslinski/Dev/cv2b/cv2b/web/node_modules/.pnpm/next@14.2.18_@babel+core@7.26.0_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/base-server.js:1353:42)
    at async cacheEntry.responseCache.get.routeKind (/home/lmyslinski/Dev/cv2b/cv2b/web/node_modules/.pnpm/next@14.2.18_@babel+core@7.26.0_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/base-server.js:1575:28)
    at async DevServer.renderToResponseWithComponentsImpl (/home/lmyslinski/Dev/cv2b/cv2b/web/node_modules/.pnpm/next@14.2.18_@babel+core@7.26.0_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/base-server.js:1483:28)
    at async DevServer.renderPageComponent (/home/lmyslinski/Dev/cv2b/cv2b/web/node_modules/.pnpm/next@14.2.18_@babel+core@7.26.0_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/base-server.js:1911:24)
    at async DevServer.renderToResponseImpl (/home/lmyslinski/Dev/cv2b/cv2b/web/node_modules/.pnpm/next@14.2.18_@babel+core@7.26.0_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/base-server.js:1949:32)
    at async DevServer.pipeImpl (/home/lmyslinski/Dev/cv2b/cv2b/web/node_modules/.pnpm/next@14.2.18_@babel+core@7.26.0_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/base-server.js:921:25)
    at async NextNodeServer.handleCatchallRenderRequest (/home/lmyslinski/Dev/cv2b/cv2b/web/node_modules/.pnpm/next@14.2.18_@babel+core@7.26.0_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/next-server.js:272:17)
    at async DevServer.handleRequestImpl (/home/lmyslinski/Dev/cv2b/cv2b/web/node_modules/.pnpm/next@14.2.18_@babel+core@7.26.0_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/base-server.js:817:17)
    at async /home/lmyslinski/Dev/cv2b/cv2b/web/node_modules/.pnpm/next@14.2.18_@babel+core@7.26.0_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/dev/next-dev-server.js:339:20
    at async Span.traceAsyncFn (/home/lmyslinski/Dev/cv2b/cv2b/web/node_modules/.pnpm/next@14.2.18_@babel+core@7.26.0_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/trace/trace.js:154:20)
    at async DevServer.handleRequest (/home/lmyslinski/Dev/cv2b/cv2b/web/node_modules/.pnpm/next@14.2.18_@babel+core@7.26.0_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/dev/next-dev-server.js:336:24)
    at async invokeRender (/home/lmyslinski/Dev/cv2b/cv2b/web/node_modules/.pnpm/next@14.2.18_@babel+core@7.26.0_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/lib/router-server.js:173:21)
    at async handleRequest (/home/lmyslinski/Dev/cv2b/cv2b/web/node_modules/.pnpm/next@14.2.18_@babel+core@7.26.0_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/lib/router-server.js:350:24)
    at async requestHandlerImpl (/home/lmyslinski/Dev/cv2b/cv2b/web/node_modules/.pnpm/next@14.2.18_@babel+core@7.26.0_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/lib/router-server.js:374:13)
    at async Server.requestListener (/home/lmyslinski/Dev/cv2b/cv2b/web/node_modules/.pnpm/next@14.2.18_@babel+core@7.26.0_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/lib/start-server.js:141:13)
 DELETE /api/organization/membership?orgId=org_2oxqsB3fT0xf6VMfn5rX413Ye7K 500 in 1258ms
[07:09:06.220] ERROR (api-error-handler/26689): API Error occurred
    error: {
      "status": 400,
      "clerkTraceId": "0128e81e3b5242bd15a9da2e54cf54eb",
      "clerkError": true,
      "errors": [
        {
          "code": "organization_minimum_permissions_needed",
          "message": "minimum organization permissions needed",
          "longMessage": "There has to be at least one organization member with the minimum required permissions",
          "meta": {}
        }
      ]
    }

Environment

System:
    OS: Linux 5.15 Ubuntu 22.04.4 LTS 22.04.4 LTS (Jammy Jellyfish)
    CPU: (12) x64 AMD Ryzen 5 5600X 6-Core Processor
    Memory: 4.50 GB / 7.72 GB
    Container: Yes
    Shell: 5.8.1 - /usr/bin/zsh
  Binaries:
    Node: 20.16.0 - /run/user/1000/fnm_multishells/952_1731821102536/bin/node
    npm: 10.8.1 - /run/user/1000/fnm_multishells/952_1731821102536/bin/npm
    pnpm: 9.7.0 - ~/.local/share/pnpm/pnpm
  npmPackages:
    @clerk/nextjs: ^6.3.4 => 6.3.4 
    @clerk/types: ^4.34.0 => 4.34.0 

    @types/node: ^20.14.1 => 20.17.6 
    @types/pg: ^8.11.6 => 8.11.10 
    @types/react: ^18.3.3 => 18.3.12 
    next: ^14.2.8 => 14.2.18
    tsx: ^4.11.2 => 4.19.2 
    typescript: ^5.4.5 => 5.6.3
@lmyslinski lmyslinski added the needs-triage A ticket that needs to be triaged by a team member label Nov 17, 2024
@panteliselef
Copy link
Member

Hey @lmyslinski thanks for discovering this bug. If you were to upgrade to next 15 the error message would change to describe the issue better. Ultimately the issue is that we re-export the isClerkAPIResponseError under a "use-client" barrier which makes is non existent in your server-only bundle for Route handlers.

We will be fixing this soon, but as a workaround please import the utility from '@clerk/shared/error' (you might need to install the package explicitly).

A follow-up question I have is, from which @clerk/nextjs are you upgrading from, since this bug seems to existed since v5.

@lmyslinski
Copy link
Author

@panteliselef Thanks for the response. I'm upgrading from:

    "@clerk/clerk-sdk-node": "^5.0.22",
    "@clerk/nextjs": "^5.2.11",
    "@clerk/types": "^4.10.0",

However I haven't used this method before, decide to improve the error handling along with the upgrade. As a workaround I've just implemented a similar method myself so I'm good for now.

@LekoArts LekoArts added nextjs and removed needs-triage A ticket that needs to be triaged by a team member labels Nov 19, 2024
@linear linear bot added bug Something isn't working and removed confirmed nextjs labels Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants