Erroneous error returning in organization delete endpoint #1789

Closed
opened 2026-03-13 09:03:39 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @pathconnected on GitHub (Aug 27, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Call /organization/delete on an organization where the current user is not member of.

Current vs. Expected behavior

The backend returns a 200 with empty body, but it should return an error:

			if (!member) {
				return ctx.json(null, {
					status: 400,
					body: {
						message:
							ORGANIZATION_ERROR_CODES.USER_IS_NOT_A_MEMBER_OF_THE_ORGANIZATION,
					},
				});
			}

What version of Better Auth are you using?

1.3.7

System info

Does not matter

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

Package

Auth config (if applicable)

Add organization plugin.

Additional context

Seems like the way the error is returned is different from how it's done elsewhere, by throwing an APIError.

Originally created by @pathconnected on GitHub (Aug 27, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Call `/organization/delete` on an organization where the current user is not member of. ### Current vs. Expected behavior The backend returns a `200` with empty body, but it should return an error: ```typescript if (!member) { return ctx.json(null, { status: 400, body: { message: ORGANIZATION_ERROR_CODES.USER_IS_NOT_A_MEMBER_OF_THE_ORGANIZATION, }, }); } ``` ### What version of Better Auth are you using? 1.3.7 ### System info ```bash Does not matter ``` ### Which area(s) are affected? (Select all that apply) Package ### Auth config (if applicable) Add organization plugin. ### Additional context Seems like the way the error is returned is different from how it's done elsewhere, by throwing an APIError.
GiteaMirror added the bug label 2026-03-13 09:03:39 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Aug 27, 2025):

Hi @pathconnected! I'm Dosu and I’m helping the better-auth team.

You're right—error handling in the /organization/delete endpoint is inconsistent with other organization routes. The standard in Better Auth is to throw an APIError for membership errors, not to return a manual JSON response. Other endpoints like update and leave already follow this pattern, which improves consistency and client error handling [docs].

To align with project standards, refactor the membership check like this:

import { APIError } from "better-auth/api";

if (!member) {
  throw new APIError("BAD_REQUEST", {
    message: ORGANIZATION_ERROR_CODES.USER_IS_NOT_A_MEMBER_OF_THE_ORGANIZATION,
  });
}

Recent PRs have started moving error handling in this direction for other cases in the delete endpoint, but the membership check still needs updating [PR #4024]. It’s a good idea to review other organization routes for similar inconsistencies and standardize on throwing APIError for all error scenarios.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

@dosubot[bot] commented on GitHub (Aug 27, 2025): <!-- Greeting --> Hi @pathconnected! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> You're right—error handling in the `/organization/delete` endpoint is inconsistent with other organization routes. The standard in Better Auth is to throw an `APIError` for membership errors, not to return a manual JSON response. Other endpoints like update and leave already follow this pattern, which improves consistency and client error handling [[docs](https://better-auth.com/docs/concepts/api)]. To align with project standards, refactor the membership check like this: ```typescript import { APIError } from "better-auth/api"; if (!member) { throw new APIError("BAD_REQUEST", { message: ORGANIZATION_ERROR_CODES.USER_IS_NOT_A_MEMBER_OF_THE_ORGANIZATION, }); } ``` Recent PRs have started moving error handling in this direction for other cases in the delete endpoint, but the membership check still needs updating [[PR #4024](https://github.com/better-auth/better-auth/pull/4024)]. It’s a good idea to review other organization routes for similar inconsistencies and standardize on throwing `APIError` for all error scenarios. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/4a926892-4cc3-437c-b3a7-fadaa2a5c511?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/4a926892-4cc3-437c-b3a7-fadaa2a5c511?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/4a926892-4cc3-437c-b3a7-fadaa2a5c511?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/4a926892-4cc3-437c-b3a7-fadaa2a5c511?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/4a926892-4cc3-437c-b3a7-fadaa2a5c511?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/4a926892-4cc3-437c-b3a7-fadaa2a5c511?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/4a926892-4cc3-437c-b3a7-fadaa2a5c511?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/4261)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1789