Module: JsonCrudApi::JsonErrors

Included in:
API
Defined in:
lib/json-crud-api/json_errors.rb

Instance Method Summary collapse

Instance Method Details

#add_error(code, message, reference = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/json-crud-api/json_errors.rb', line 10

def add_error(code, message, reference = nil)
  @errors = [] if @errors.nil?

  error = {
    :code => code,
    :message => message,
  }
  error[:reference] = reference unless reference.nil?
  @errors.push error
end

#clear_errorsObject



6
7
8
# File 'lib/json-crud-api/json_errors.rb', line 6

def clear_errors 
  @errors = []
end

#fail_forbiddenObject



41
42
43
# File 'lib/json-crud-api/json_errors.rb', line 41

def fail_forbidden
  fail_with_error 403, 'FORBIDDEN','The user is not allowed to perform this operation on the resource.'
end

#fail_not_foundObject



33
34
35
# File 'lib/json-crud-api/json_errors.rb', line 33

def fail_not_found
  fail_with_errors 404
end

#fail_unauthorizedObject



37
38
39
# File 'lib/json-crud-api/json_errors.rb', line 37

def fail_unauthorized
  fail_with_error 401, 'UNAUTHORIZED','Authorization is required to perform this operation on the resource.'
end

#fail_with_error(status, code, message, reference = nil) ⇒ Object



21
22
23
24
# File 'lib/json-crud-api/json_errors.rb', line 21

def fail_with_error(status, code, message, reference = nil)
  add_error code,message,reference
  fail_with_errors status
end

#fail_with_errors(status = 422) ⇒ Object



26
27
28
29
30
31
# File 'lib/json-crud-api/json_errors.rb', line 26

def fail_with_errors(status = 422)
  halt status, JSON.fast_generate({
    :success => false,
    :errors => @errors
  })
end