Skip to content

SDK does not throw errors instead returns undefined #14495

@Siebe-Studio

Description

@Siebe-Studio

Describe the Bug

SDK package version 3.62.1

When a Payload API request fails (e.g., 400 Bad Request), the PayloadSDK returns undefined instead of throwing an error. This makes it impossible to properly handle API errors and provides a poor developer experience.

Expected Behavior

When an API request fails, the SDK should throw an error that can be caught and handled appropriately. This would allow:

  • Proper error handling in try/catch blocks
  • React Query's onError callback to fire
  • Users to see appropriate error messages instead of success messages

Actual Behavior

The SDK returns undefined when a request fails, causing:

  • No way to easily access error details from the API response
  • Silent failures that are difficult to debug
import { type Config } from '@/payload-types'
import { PayloadSDK } from '@payloadcms/sdk'

export const sdk = new PayloadSDK<Config>({
  baseURL: process.env.NEXT_PUBLIC_SERVER_URL + '/api',
  baseInit: {
    credentials: 'include',
  },
  fetch: typeof window !== 'undefined' ? window.fetch.bind(window) : globalThis.fetch,
})

export default sdk
Image
{
    "errors": [
        {
            "name": "ValidationError",
            "data": {
                "collection": "waitlist-email",
                "errors": [
                    {
                        "message": "Value must be unique",
                        "path": "email"
                    }
                ]
            },
            "message": "The following field is invalid: email"
        }
    ]
}
Image

Link to the code that reproduces this issue

see below

Reproduction Steps

  1. Set up a Payload SDK instance with a custom fetch
  2. Attempt to create a document that violates validation (e.g., duplicate unique field)
  3. Observe that the SDK returns undefined instead of throwing an error
import { PayloadSDK } from '@payloadcms/sdk'

const sdk = new PayloadSDK<Config>({
  baseURL: process.env.NEXT_PUBLIC_SERVER_URL + '/api',
  baseInit: {
    credentials: 'include',
  },
  fetch: typeof window !== 'undefined' ? window.fetch.bind(window) : globalThis.fetch,
})

// This returns undefined when the API returns 400 Bad Request
const response = await sdk.create({
  collection: 'waitlist-email',
  data: {
    email: 'duplicate@example.com', // Assuming this email already exists
  },
})

console.log(response) // undefined

API Response

The API correctly returns a 400 Bad Request with error details:

{
  "errors": [
    {
      "name": "ValidationError",
      "data": {
        "collection": "waitlist-email",
        "errors": [
          {
            "message": "Value must be unique",
            "path": "email"
          }
        ]
      },
      "message": "The following field is invalid: email"
    }
  ]
}

However, this error information is lost because the SDK returns undefined instead of throwing an error with this data.

Which area(s) are affected? (Select all that apply)

plugin: other

Environment Info

Binaries:
  Node: 22.14.0
  npm: 10.9.2
  Yarn: N/A
  pnpm: 10.6.5
Relevant Packages:
  payload: 3.62.1
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.6.0: Mon Jul 14 11:30:40 PDT 2025; root:xnu-11417.140.69~1/RELEASE_ARM64_T6041
  Available memory (MB): 24576
  Available CPU cores: 12

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions