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

feat: add status message based on status code common errors #978

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

IqroNegoro
Copy link

as the title say, i just add default statusMessage based on statusCode, i kinda not like that statusMessage is always empty and must explicitly add in any createError to show it up. especially in Nuxt server api response.

assume we have some error :

} catch (error : any) {
    // return fields error and it message if validation error but not with statusMessage "Bad Request"
    if (error instanceof ValidationError) {
        throw createError({
           statusCode: 400,
           message: "Validation Error",
           data: {
             field: 'error message',
             // ...another error
          });
    }
    // return error 500 with Internal Server Error status message
   throw createError({
      statusCode: 500,
      statusMessage: "Internal Server Error",
      message: "Internal Server Error"
    });
}

with default statusMessage, we not have to explicitly add statusMessage on any createError, default statusMessage with handle it.

throw createError({
  statusCode: 400,
  message: "Validation Error",
  data: {
    field: 'error message',
    // ...another error
});

// response HTTP
{
  statusCode: 400,
  statusMessage: "Bad Request"
  // ... another field
}

just reducing unnecessary repetition for good http responses...

with pnpm dev for testing any errors :

image

@IqroNegoro IqroNegoro requested a review from pi0 as a code owner February 20, 2025 18:01
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

Successfully merging this pull request may close these issues.

1 participant