class ViewModel::AbstractError

Abstract base for renderable errors in ViewModel-based APIs. Errors of this type will be caught by ViewModel controllers and rendered in a standard format by ViewModel::ErrorView, which loosely follows errors in JSON-API.

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/view_model/error.rb, line 16
def initialize
  # `detail` is used to provide the exception message. However, it's not safe
  # to just override StandardError's `message` or `to_s` to call `detail`,
  # since some of Ruby's C implementation of Exceptions internally ignores
  # these methods and fetches the invisible internal `idMesg` attribute
  # instead. (!)
  #
  # This means that all fields necessary to derive the detail message must be
  # initialized before calling super().
  super(detail)
end

Public Instance Methods

aggregation?() click to toggle source

Some types of error may be aggregations over multiple causes

# File lib/view_model/error.rb, line 54
def aggregation?
  false
end
causes() click to toggle source

If so, the causes of this error (as AbstractErrors)

# File lib/view_model/error.rb, line 59
def causes
  nil
end
code() click to toggle source

Unique symbol identifying this error type

# File lib/view_model/error.rb, line 44
def code
  'ViewModel.AbstractError'
end
detail() click to toggle source

Human-readable reason for use displaying this error.

# File lib/view_model/error.rb, line 29
def detail
  'ViewModel::AbstractError'
end
exception() click to toggle source

The exception responsible for this error. In most cases, that should be this object, but sometimes an Error may be used to wrap an external exception.

# File lib/view_model/error.rb, line 65
def exception
  self
end
meta() click to toggle source

Additional information specific to this error type.

# File lib/view_model/error.rb, line 49
def meta
  {}
end
status() click to toggle source

HTTP status code most appropriate for this error

# File lib/view_model/error.rb, line 34
def status
  500
end
title() click to toggle source

Human-readable title for displaying this error

# File lib/view_model/error.rb, line 39
def title
  nil
end
to_s() click to toggle source
# File lib/view_model/error.rb, line 73
def to_s
  detail
end
view() click to toggle source
# File lib/view_model/error.rb, line 69
def view
  ViewModel::ErrorView.new(self)
end

Protected Instance Methods

format_reference(viewmodel_ref) click to toggle source
# File lib/view_model/error.rb, line 85
def format_reference(viewmodel_ref)
  {
    ViewModel::TYPE_ATTRIBUTE => viewmodel_ref.viewmodel_class.view_name,
    ViewModel::ID_ATTRIBUTE   => viewmodel_ref.model_id,
  }
end
format_references(viewmodel_refs) click to toggle source
# File lib/view_model/error.rb, line 79
def format_references(viewmodel_refs)
  viewmodel_refs.map do |viewmodel_ref|
    format_reference(viewmodel_ref)
  end
end