zaengle/error-handler

Override default error messages

Closed this issue ยท 3 comments

Hello. Is it somehow possible to override the default status codes and the corresponding messages from within the package?

@GabrielHangor Can you please provide more information about your use case? Are you perhaps asking about a configuration option?

On an instance-by-instance case, you can provide custom status codes and messages like the example seen here.

If you're using a Nuxt plugin, for example, you can provide custom status codes and messages for every instance by doing something like this:

// plugins/error-handler.js

import ErrorHandler from '@zaengle/error-handler'

export default (context, inject) => {
  inject(
    'errorHandler',
    new ErrorHandler(
      // Custom messages for specific statuses
      {
        401: `Whoops! You can't access that.`,
        404: `Uh oh! We couldn't find that.`,
        500: `Oh no! Server problem.`,
      },
      // Custom default message (if there's no corresponding status message)
      'Yikes! Something went wrong.',
    )
  )
}

@GabrielHangor Can you please provide more information about your use case? Are you perhaps asking about a configuration option?

On an instance-by-instance case, you can provide custom status codes and messages like the example seen here.

If you're using a Nuxt plugin, for example, you can provide custom status codes and messages for every instance by doing something like this:

// plugins/error-handler.js

import ErrorHandler from '@zaengle/error-handler'

export default (context, inject) => {
  inject(
    'errorHandler',
    new ErrorHandler(
      // Custom messages for specific statuses
      {
        401: `Whoops! You can't access that.`,
        404: `Uh oh! We couldn't find that.`,
        500: `Oh no! Server problem.`,
      },
      // Custom default message (if there's no corresponding status message)
      'Yikes! Something went wrong.',
    )
  )
}

Thx for the answer. This should definitely help and yes i am using nuxt, so i am just gonna provide custom messages from within the plugin.
What i meant is if it is possible to override the default messages so i dont have to provide my own every time i call this method. But with the plugin approach there is no need to do so. Thank you! Working like a champ now.

Great! I'll close this but feel free to reopen it if you have more questions. ๐Ÿ™‚