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

Status code in APIError #1

Open
hurby24 opened this issue Jan 21, 2025 · 2 comments
Open

Status code in APIError #1

hurby24 opened this issue Jan 21, 2025 · 2 comments

Comments

@hurby24
Copy link

hurby24 commented Jan 21, 2025

Shouldn't APIError return a response with the correct error code?

this.status = status;
		this.body = body ?? {};
		this.body.code = body?.message
			? body.message
					.toUpperCase()
					.replace(/ /g, "_")
					.replace(/[^A-Z0-9_]/g, "")
			: status;

https://github.com/Bekacru/better-call/blob/main/src/error.ts
Instead it either returns a plain status string or message if it exists.

@hurby24
Copy link
Author

hurby24 commented Jan 21, 2025

https://github.com/Bekacru/better-call/blob/main/src/router.ts#L145
Even though it returns the error code by using status as a key, I still get error like this

{
  code : "NOT_FOUND"
}

instead of

{
  code : 404
}

@opswiz
Copy link

opswiz commented Jan 31, 2025

in my case, we try to return custom error code like apart from status code for application logics

new ApiError("CONFLICT", {
  code: "TENANTS_NOT_AVAILABLE", // won't work
  err_code: "TENANTS_NOT_AVAILABLE"
  // message could be even longer
  message: "Tenants are not available under your current plan. Please upgrade to access tenant features." 
})

currently, the message is as is formatted as code, though we are adding custom keys for that, but having message formatted as code is still confusing and long

For example,

{
  "err_code": "TENANTS_NOT_AVAILABLE",
  "message": "Tenants are not available under your current plan. Please upgrade to access tenant features.",
  "code": "TENANTS_ARE_NOT_AVAILABLE_UNDER_YOUR_CURRENT_PLAN_PLEASE_UPGRADE_TO_ACCESS_TENANT_FEATURES"
}

Allowing code to be overridden will help avoid having extra keys and also avoid having messages like this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants