Exception: ViewModel::AbstractError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/view_model/error.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initializeAbstractError

Returns a new instance of AbstractError.



16
17
18
19
20
21
22
23
24
25
26
# 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

Instance Method Details

#aggregation?Boolean

Some types of error may be aggregations over multiple causes

Returns:

  • (Boolean)


54
55
56
# File 'lib/view_model/error.rb', line 54

def aggregation?
  false
end

#causesObject

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



59
60
61
# File 'lib/view_model/error.rb', line 59

def causes
  nil
end

#codeObject

Unique symbol identifying this error type



44
45
46
# File 'lib/view_model/error.rb', line 44

def code
  'ViewModel.AbstractError'
end

#detailObject

Human-readable reason for use displaying this error.



29
30
31
# File 'lib/view_model/error.rb', line 29

def detail
  'ViewModel::AbstractError'
end

#exceptionObject

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.



65
66
67
# File 'lib/view_model/error.rb', line 65

def exception
  self
end

#metaObject

Additional information specific to this error type.



49
50
51
# File 'lib/view_model/error.rb', line 49

def meta
  {}
end

#statusObject

HTTP status code most appropriate for this error



34
35
36
# File 'lib/view_model/error.rb', line 34

def status
  500
end

#titleObject

Human-readable title for displaying this error



39
40
41
# File 'lib/view_model/error.rb', line 39

def title
  nil
end

#to_sObject



73
74
75
# File 'lib/view_model/error.rb', line 73

def to_s
  detail
end

#viewObject



69
70
71
# File 'lib/view_model/error.rb', line 69

def view
  ViewModel::ErrorView.new(self)
end