class Onyx::HTTP::Middleware::Renderer

Overview

A handler which renders either context.response.error or context.response.view if it is present. Errors rendering is determined by the "Accept" request header and can be either "application/json", "text/plain" (default) or "text/html".

Included Modules

Defined in:

onyx-http/middleware/renderer.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(verbose : Bool = true) #

You are likely to set verbose to false in production mode to hide actual error payloads.


[View source]

Instance Method Detail

def call(context) #

[View source]
def render_html_error(context, error : Error) #

Render an Onyx::HTTP::Error into a minimalistic HTML page. It uses Kilt under the hood.


[View source]
def render_html_error(context, error : Exception) #

Render an exception into HTML. It uses Kilt under the hood. Based on the @verbose variable, the output differs. If it is true, then a debug page is output powered by ExceptionPage shard. Otherwise, a minimalistic HTML page is rendered with no details.


[View source]
def render_json_error(context, error : Error) #

Render Onyx::HTTP::Error into JSON. Example:

{
  "error": {
    "name": "User Not Found",
    "message": "User not found with ID 42",
    "code": 404,
    "payload": {
      "id": 42
    }
  }
}

[View source]
def render_json_error(context, error : Exception) #

Render an exception into JSON. Based on the @verbosity variable, the output differs.

Example with @verbose = true:

{
  "error": {
    "name": "Division By Zero",
    "message": "Division by zero",
    "code": 500,
    "payload": {
      "backtrace": ["<error_backtrace>"]
    }
  }
}

With @verbose = false:

{
  "error": {
    "name": "Exception",
    "message": null,
    "code": 500,
    "payload": null
  }
}

[View source]
def render_text_error(context, error : Error) #

Render an Onyx::HTTP::Error into a plain text. If the error contains a message, it is print with dash:

404 User Not FoundUser not found with ID 42

[View source]
def render_text_error(context, error : Exception) #

Render an exception into a plain text. The output differs depending on the @verbose variable. If it is set to true, then a full error message with backtrace is printed. Otherwise, a simple 500 Internal Server Error message is printed.


[View source]