Class: Shamu::JsonApi::ErrorBuilder

Inherits:
Object
  • Object
show all
Includes:
BuilderMethods::Link, BuilderMethods::Meta
Defined in:
lib/shamu/json_api/error_builder.rb

Overview

Build an error response object.

Instance Method Summary collapse

Methods included from BuilderMethods::Meta

#meta

Methods included from BuilderMethods::Link

#link

Constructor Details

#initializeErrorBuilder

Returns a new instance of ErrorBuilder.



9
10
11
# File 'lib/shamu/json_api/error_builder.rb', line 9

def initialize
  @output = {}
end

Instance Method Details

#code(code) ⇒ self

Set an application specific error code.

Returns:

  • (self)


46
47
48
49
# File 'lib/shamu/json_api/error_builder.rb', line 46

def code( code )
  output[:code] = code.to_s
  self
end

#compileHash

Returns the results output as JSON safe hash.

Returns:

  • (Hash)

    the results output as JSON safe hash.



83
84
85
# File 'lib/shamu/json_api/error_builder.rb', line 83

def compile
  output
end

#detail(message) ⇒ String, self

Returns:

  • (String)

    message details about the error.

  • (self)


60
61
62
63
# File 'lib/shamu/json_api/error_builder.rb', line 60

def detail( message )
  output[:detail] = message
  self
end

#exception(exception) ⇒ self

Summarize an exception as an error.

Parameters:

  • exception (Exception)

Returns:

  • (self)


26
27
28
29
30
31
32
33
# File 'lib/shamu/json_api/error_builder.rb', line 26

def exception( exception )
  name = exception.class.name.demodulize.gsub( /Error$/, "" )
  code name.underscore
  title name.titleize
  detail exception.message

  self
end

#http_status(status) ⇒ self

Set an HTTP status code related to the error.

Parameters:

  • status (Symbol, Integer)

    code.

Returns:

  • (self)


38
39
40
41
42
# File 'lib/shamu/json_api/error_builder.rb', line 38

def http_status( status )
  status = ::Rack::Utils.status_code( status ) if status.is_a? Symbol
  output[:status] = status.to_s
  self
end

#id(id) ⇒ self

Parameters:

  • id (String)

    unique id for this occurrence of the error.

Returns:

  • (self)


18
19
20
21
# File 'lib/shamu/json_api/error_builder.rb', line 18

def id( id )
  output[:id] = id
  self
end

#parameter(name) ⇒ self

The name of the parameter that caused the error.

Returns:

  • (self)


76
77
78
79
80
# File 'lib/shamu/json_api/error_builder.rb', line 76

def parameter( name )
  output[:source] ||= {}
  output[:source][:parameter] = name
  self
end

#pointer(pointer) ⇒ self

JSON pointer to the associated document in the request that was the source of the pointer.

Returns:

  • (self)


68
69
70
71
72
# File 'lib/shamu/json_api/error_builder.rb', line 68

def pointer( pointer )
  output[:source] ||= {}
  output[:source][:pointer] = pointer
  self
end

#title(title) ⇒ self

Set a short human readable title of the error.

Returns:

  • (self)


53
54
55
56
# File 'lib/shamu/json_api/error_builder.rb', line 53

def title( title )
  output[:title] = title.to_s
  self
end