Skip to content

feat(sdk): add proper error handling - #15143

Merged
AlessioGr merged 3 commits into
feat/drizzle-validation-error-slugsfrom
feat/sdk-error
Jan 9, 2026
Merged

AlessioGr merged 3 commits into
feat/drizzle-validation-error-slugsfrom
feat/sdk-error

Conversation

@AlessioGr

@AlessioGr AlessioGr commented Jan 9, 2026 •

Copy link
Copy Markdown
Member

DO NOT MERGE BEFORE #15147

Fixes #14495

The SDK now throws a proper PayloadSDKError class on failed API requests instead of either returning undefined or a generic Error. Error handling is centralized in the request() method, which adds error handling for operations where it was previously missing (like create, update, delete, find, etc.).

Changes:

  • Added PayloadSDKError class with status, errors, response, and message properties
  • Centralized error handling in request() - all operations now properly throw on failed responses. Previously, some operations (like findByID) just threw a generic Error that did not explain what actually went wrong and some operations (like create) did not throw an error at all and simply returned undefined.
  • Lots of new int tests

Usage:

import { PayloadSDKError } from '@payloadcms/sdk'

try {
  await sdk.create({ collection: 'posts', data: { ... } })
} catch (err) {
  if (err instanceof PayloadSDKError) {
    console.log(err.status)  // 400
    console.log(err.errors)  // [{ name: 'ValidationError', message: '...', data: {...} }]
  }
}

} & Partial<BusboyConfig>

export type ErrorResult = { data?: any; errors: unknown[]; stack?: string }
export type ErrorResult = {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type was too generic before

}
} catch {
return response.json()
} catch (err) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The remaining try { } catch { } es are only there to support disableErrors.

Comment thread packages/sdk/src/index.ts

const response = await this.fetch(`${this.baseURL}${path}${buildSearchParams(args)}`, init)

if (!response.ok) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Central error handling right here. That way, we don't need to check for response.ok in every single operation.

@AlessioGr
AlessioGr requested a review from r1tsuu January 9, 2026 02:14
@AlessioGr
AlessioGr enabled auto-merge (squash) January 9, 2026 02:14
@github-actions

github-actions Bot commented Jan 9, 2026

Copy link
Copy Markdown
Contributor

📦 esbuild Bundle Analysis for payload

This analysis was generated by esbuild-bundle-analyzer. 🤖
This PR introduced no changes to the esbuild bundle! 🙌

@AlessioGr
AlessioGr changed the base branch from main to feat/drizzle-validation-error-slugs January 9, 2026 05:03
@AlessioGr
AlessioGr merged commit 2d9deef into feat/drizzle-validation-error-slugs Jan 9, 2026
@AlessioGr
AlessioGr deleted the feat/sdk-error branch January 9, 2026 05:03
AlessioGr added a commit that referenced this pull request Jan 9, 2026
AlessioGr added a commit that referenced this pull request Jan 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SDK does not throw errors instead returns undefined

1 participant