class Radbas::HTTPError

Overview

Raised to end a request with an HTTP error status.

CommonErrorHandler answers it with Problem Details (RFC 9457): message becomes #detail, type and title the members of the same name. With no type (about:blank), #title is always the status description. headers are sent with the error response, e.g. WWW-Authenticate for 401 or Allow for 405.

raise Radbas::HTTPError.new(:not_found)
raise Radbas::HTTPError.new(:bad_request, "missing name")
raise Radbas::HTTPError.new(:forbidden, "balance too low", type: "https://example.com/probs/out-of-credit", title: "Out of credit")

Subclasses add members to the body by overriding #write_extensions.

Defined in:

radbas-framework/http_error.cr

Constant Summary

ABOUT_BLANK = "about:blank"

Constructors

Instance Method Summary

Constructor Detail

def self.new(status : HTTP::Status, message : String | Nil = nil, headers : HTTP::Headers = HTTP::Headers.new, cause : Exception | Nil = nil, type : String | Nil = nil, title : String | Nil = nil) #

[View source]

Instance Method Detail

def detail : String | Nil #

The message as given, nil if none was given.


[View source]
def headers : HTTP::Headers #

[View source]
def status : HTTP::Status #

[View source]
def title : String #

A short summary of the problem type.


[View source]
def type : String #

The problem type, a URI reference.


[View source]
def write_extensions(json : JSON::Builder) : Nil #

Writes extension members into the Problem Details object, nothing by default.

class ValidationProblem < Radbas::HTTPError
  def initialize(@errors : Array(FieldError))
    super(:unprocessable_entity, "invalid input")
  end

  def write_extensions(json : JSON::Builder) : Nil
    json.field("errors") { @errors.to_json(json) }
  end
end

[View source]