Skip to content

Commit

Permalink
Rename: errors.rb exception_handlers.rb. Update specs. rescue_from :all
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaroslav Savchuk committed Feb 18, 2019
1 parent 1f13cab commit e6ed15e
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 65 deletions.
4 changes: 1 addition & 3 deletions app/api/v2/account/deposits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ class Deposits < Grape::API
desc: "Deposit transaction id"
end
get "/deposits/:txid" do
deposit = current_user.deposits.find_by(txid: params[:txid])
raise DepositByTxidNotFoundError, params[:txid] unless deposit

deposit = current_user.deposits.find_by!(txid: params[:txid])
present deposit, with: API::V2::Entities::Deposit
end

Expand Down
28 changes: 0 additions & 28 deletions app/api/v2/errors.rb

This file was deleted.

31 changes: 31 additions & 0 deletions app/api/v2/exception_handlers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# encoding: UTF-8
# frozen_string_literal: true

module API::V2
module ExceptionHandlers
def self.included(base)
base.instance_eval do
rescue_from Grape::Exceptions::ValidationErrors do |e|
errors_array = e.full_messages.map do |err|
err.split.last
end
error!({ errors: errors_array }, 422)
end

rescue_from Peatio::Auth::Error do |e|
report_exception(e)
error!({ errors: ['jwt.decode_and_verify'] }, 401)
end

rescue_from ActiveRecord::RecordNotFound do |_e|
error!({ errors: ['record.not_found'] }, 404)
end

rescue_from :all do |e|
report_exception(e)
error!({ errors: ['server.internal_error'] }, 500)
end
end
end
end
end
25 changes: 0 additions & 25 deletions app/api/v2/new_exceptions_handlers.rb

This file was deleted.

2 changes: 2 additions & 0 deletions spec/api/v2/account/deposits_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@
it 'return 404 if txid not exist' do
api_get '/api/v2/account/deposits/5', token: token
expect(response.code).to eq '404'
expect(response).to include_api_error('record.not_found')
end

it 'returns 404 if txid not belongs_to you ' do
api_get '/api/v2/account/deposits/10', token: token
expect(response.code).to eq '404'
expect(response).to include_api_error('record.not_found')
end

it 'returns deposit txid if exist' do
Expand Down
6 changes: 3 additions & 3 deletions spec/api/v2/auth/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ class Mount
it 'should deny access when token is not given' do
api_get '/api/v2/'
expect(response.code).to eq '401'
expect(response.body).to eq '{"error":{"code":2001,"message":"Authorization failed"}}'
expect(response).to include_api_error('jwt.decode_and_verify')
end

it 'should deny access when invalid token is given' do
api_get '/api/v2/', token: '123.456.789'
expect(response.code).to eq '401'
expect(response.body).to eq '{"error":{"code":2001,"message":"Authorization failed"}}'
expect(response).to include_api_error('jwt.decode_and_verify')
end

it 'should allow access when valid token is given' do
Expand All @@ -51,7 +51,7 @@ class Mount
it 'should deny access' do
api_get '/api/v2/'
expect(response.code).to eq '401'
expect(response.body).to eq '{"error":{"code":2001,"message":"Authorization failed"}}'
expect(response).to include_api_error('jwt.decode_and_verify')
end
end
end
2 changes: 1 addition & 1 deletion spec/api/v2/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Mount
get '/api/v2/auth_test'

expect(response.code).to eq '401'
expect(response.body).to eq '{"error":{"code":2001,"message":"Authorization failed"}}'
expect(response).to include_api_error('jwt.decode_and_verify')
end
end
end
Expand Down
24 changes: 19 additions & 5 deletions spec/api/v2/mount_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ module API
module V2
class Mount
get('/null') { '' }
get('/broken') { raise Error, code: 2_014_310, text: 'MtGox bankrupt' }
get('/record-not-found') { raise ActiveRecord::RecordNotFound }
get('/auth-error') { raise Peatio::Auth::Error }
get('/standard-error') { raise StandardError }
end
end
end
Expand All @@ -17,10 +19,22 @@ class Mount
end

context 'handle exception on request processing' do
it 'should render json error message' do
get '/api/v2/broken'
expect(response.code).to eq '400'
expect(JSON.parse(response.body)).to eq('error' => { 'code' => 2_014_310, 'message' => 'MtGox bankrupt' })
it 'returns array with record.not_found error' do
get '/api/v2/record-not-found'
expect(response.code).to eq '404'
expect(response).to include_api_error('record.not_found')
end

it 'returns array with jwt.decode_and_verify error' do
get '/api/v2/auth-error'
expect(response.code).to eq '401'
expect(response).to include_api_error('jwt.decode_and_verify')
end

it 'returns array with server.internal_error error' do
get '/api/v2/standard-error'
expect(response.code).to eq '500'
expect(response).to include_api_error('server.internal_error')
end
end

Expand Down

0 comments on commit e6ed15e

Please sign in to comment.