Guest User

Untitled

a guest
Apr 20th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. # frozen_string_literal: true
  2.  
  3. module Handler
  4. # MATCHERS = []
  5. def handle(result)
  6. return result[:model] if result.success?
  7. return failure(result) if result.failure?
  8. error!('Something went wrong! Please, try again or contact the developer!')
  9. end
  10.  
  11. def failure(result)
  12. return error!('Forbidden', 403) if result['result.policy.default']&.failure?
  13. return error!('Not Found', 404) if result['result.model']&.failure?
  14. return error!(result[:errors][:message], result[:errors][:status]) if result[:errors]
  15. error!(result['contract.default'].errors.full_messages.join(', '), 422)
  16. end
  17.  
  18. def error!(msg, status = nil)
  19. raise RequestError.new(msg, status)
  20. end
  21.  
  22. class RequestError < StandardError
  23.  
  24. attr_reader :status
  25.  
  26. def initialize(message, status = nil)
  27. @status = status || 400
  28. super(message)
  29. end
  30. end
  31. end
Add Comment
Please, Sign In to add comment