Skip to content

Commit

Permalink
Add mutant and simplify errors
Browse files Browse the repository at this point in the history
  • Loading branch information
iwdt committed Feb 27, 2024
1 parent 46b7e63 commit 4d20139
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 24 deletions.
21 changes: 21 additions & 0 deletions .mutant.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
includes:
- lib
requires:
- shikimori-api
- shikimori-oauth2
- omniauth-shikimori-oauth2

matcher:
subjects:
- Shikimori::API*
- Shikimori::OAuth2*
- Omniauth::Strategies::Shikimori
- Omniauth::Shikimori*
integration:
name: rspec
arguments:
- --fail-fast
- --seed
- '0'
- spec
fail_fast: true
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ group :development, :test do

gem 'steep', require: false
gem 'typeprof', require: false

gem 'mutant-license', source: 'https://oss:[email protected]'
gem 'mutant-rspec'
end

group :test do
Expand Down
22 changes: 21 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ PATH
shikimori-oauth2 (1.0.1)
oauth2 (~> 2.0.0)

GEM
remote: https://oss:[email protected]/
specs:
mutant-license (0.1.1.2.1808796712161246693429340807553770173020.0)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -58,6 +63,15 @@ GEM
logger (1.6.0)
minitest (5.22.2)
multi_xml (0.6.0)
mutant (0.11.28)
diff-lcs (~> 1.3)
parser (~> 3.3.0)
regexp_parser (~> 2.8.2)
sorbet-runtime (~> 0.5.0)
unparser (~> 0.6.9)
mutant-rspec (0.11.28)
mutant (= 0.11.28)
rspec-core (>= 3.8.0, < 4.0.0)
mutex_m (0.2.0)
net-http (0.4.1)
uri
Expand Down Expand Up @@ -94,7 +108,7 @@ GEM
ffi (~> 1.0)
rbs (3.4.4)
abbrev
regexp_parser (2.9.0)
regexp_parser (2.8.3)
rexml (3.2.6)
rspec (3.13.0)
rspec-core (~> 3.13.0)
Expand Down Expand Up @@ -150,6 +164,7 @@ GEM
snaky_hash (2.0.1)
hashie
version_gem (~> 1.1, >= 1.1.1)
sorbet-runtime (0.5.11274)
steep (1.6.0)
activesupport (>= 5.1)
concurrent-ruby (>= 1.1.10)
Expand All @@ -173,6 +188,9 @@ GEM
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
unparser (0.6.13)
diff-lcs (~> 1.3)
parser (>= 3.3.0)
uri (0.13.0)
vcr (6.2.0)
version_gem (1.1.3)
Expand All @@ -187,6 +205,8 @@ PLATFORMS

DEPENDENCIES
dotenv
mutant-license!
mutant-rspec
omniauth-shikimori-oauth2!
rack-test
rake
Expand Down
13 changes: 1 addition & 12 deletions lib/shikimori/api/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,8 @@
module Shikimori
module API
class RequestError < StandardError; end

class NotFoundError < RequestError; end
class ForbiddenError < RequestError; end

# Error, when request is bad
class BadRequestError < RequestError
attr_reader :errors

def initialize(errors)
@errors = errors.is_a?(Hash) ? errors['errors'] : errors

super("Bad request. Errors:\n\t#{@errors.join("\n\t")}")
end
end
class BadRequestError < RequestError; end
end
end
16 changes: 12 additions & 4 deletions lib/shikimori/api/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,29 @@ def start_request(uri, request, body = nil, **options)

def parse_response(response)
case response
when Net::HTTPNoContent
{}
when Net::HTTPSuccess, Net::HTTPCreated
json_parse_response_body(response.body)
when Net::HTTPForbidden
raise ForbiddenError
when Net::HTTPNotFound
raise NotFoundError
when Net::HTTPUnprocessableEntity, Net::HTTPBadRequest
raise BadRequestError, json_parse_response_body(response.body)
raise BadRequestError, json_parse_response_errors(response.body)
end
end

def json_parse_response_errors(body)
error = json_parse_response_body(body)

if error.is_a?(Array)
error.join(', ')
elsif error.is_a?(Hash) && error.key?('errors')
error['errors']
end
end

def json_parse_response_body(body)
JSON.parse(body&.empty? ? '{}' : body)
JSON.parse(body.nil? || body.empty? ? '{}' : body)
rescue JSON::ParserError
{}
end
Expand Down
2 changes: 2 additions & 0 deletions lib/shikimori/oauth2/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module Shikimori
module OAuth2
# Configuration for Shikimori OAuth2 client
class Config
# @return [String] Default site url
DEFAULT_SITE_URL = 'https://shikimori.one/'
# @return [String] Default APP name
DEFAULT_APP_NAME = 'Api Test'

attr_accessor :options, :site, :app_name
Expand Down
3 changes: 0 additions & 3 deletions sig/shikimori/api.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ module Shikimori
end

class BadRequestError < RequestError
attr_reader errors: Array[String]

def initialize: (Array[String] | { "errors" => Array[String] }) -> void
end
end
end
7 changes: 4 additions & 3 deletions sig/shikimori/api/rest.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ module Shikimori

private

def request: (request_method, ::URI::Generic, ?body, **untyped) -> [Net::HTTPResponse, (::Hash[_ToS, Object] | nil)]
def request: (request_method, ::URI::Generic, ?body, **untyped) -> [Net::HTTPResponse, (::Hash[_ToS, Object] | nil | Array[untyped])]
def build_request: (request_method, ::URI::Generic, **untyped) -> request
def start_request: (::URI::Generic, request, ?body, **untyped) -> Net::HTTPResponse
def parse_response: (Net::HTTPResponse response) -> (::Hash[_ToS, untyped] | nil)
def json_parse_response_body: (String) -> (::Hash[_ToS, untyped] | nil)
def parse_response: (Net::HTTPResponse response) -> (::Hash[_ToS, untyped] | Array[untyped] | nil)
def json_parse_response_errors: (String) -> (String | nil)
def json_parse_response_body: (String) -> (::Hash[_ToS, untyped] | Array[untyped] | nil)
def headers_from: (Hash[untyped, untyped]) -> Hash[String, String]
def query_params_from: (::URI::Generic, Hash[untyped, untyped]) -> untyped
end
Expand Down
2 changes: 1 addition & 1 deletion spec/shikimori/api/v1/achievements_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
it {
expect { request }.to raise_error(
Shikimori::API::BadRequestError,
/Bad request. Errors:/
"Invalid parameter 'user_id' value \"$bad_format\": Must be a number."
)
}
end
Expand Down

0 comments on commit 4d20139

Please sign in to comment.