createError statusMessage vs message
with createError what is the difference between statusMessage and message?
4 Replies
Hi @Mads. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
The difference between the statusMessage and message properties in createError is primarily about their intended use and content:
- statusMessage: This property is meant for short, HTTP-compliant status texts, such as "Not Found" or "Unauthorized". It should only contain horizontal tabs, spaces, and visible ASCII characters (
- Use statusMessage for short, HTTP status texts.
- Use message for detailed error descriptions. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
[\t\u0020-\u007E]
). The value of statusMessage will be sanitized by h3, and using longer or non-ASCII content will trigger a warning. It is suitable for brief, standard status descriptions that might be shown in HTTP responses or UI status badges. For example:
- message: This property is intended for longer, more descriptive error messages. You should use message for detailed explanations, multi-line content, or any text that goes beyond a short status. This is the property to use for user-facing error details or logs. For example:
Important: If you use statusMessage for longer or non-ASCII messages, you will get a warning, and the value will be sanitized. Always use message for detailed or user-facing error information, and reserve statusMessage for short, HTTP-compliant texts only. This distinction is documented in both the Nuxt and h3 documentation and has been clarified due to developer confusion and warnings in recent versions createError: The statusMessage parameter causes a deprecation warning docs: Clarify statusMessage limitations.
In summary:- Use statusMessage for short, HTTP status texts.
- Use message for detailed error descriptions. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
<@335112909350895632> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai How do I retrieve the error on a page, for example, if I hit the endpoint using $fetch? What types of errors can be caught in a try catch?
You may need to change the accept header to application/json