Skip to content

Commit

Permalink
Move error name to BaseResponseError class
Browse files Browse the repository at this point in the history
The "friendly" name of the error that's sent as a query param (?error=friendly_name)
on error responses is, by default, the name of the error class, formatted to snake case.

This change moves this definition to a method in the base class
`BaseResponseError#name_for_response`

This way, if ever needed, the `name_for_response` can be overridden by doing:
```rb
SomeName = Class.new(BaseResponseError) do
  def self.name_for_response
    :some_other_name
  end
end
```
  • Loading branch information
camero2734 committed Nov 28, 2023
1 parent 8c513db commit bdf3d50
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
7 changes: 5 additions & 2 deletions lib/doorkeeper/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ class BaseResponseError < DoorkeeperError
def initialize(response)
@response = response
end

def self.name_for_response
self.name.demodulize.underscore.to_sym
end
end

UnableToGenerateToken = Class.new(DoorkeeperError)
TokenGeneratorNotFound = Class.new(DoorkeeperError)
NoOrmCleaner = Class.new(DoorkeeperError)

ServerError = Class.new(BaseResponseError)

InvalidRequest = Class.new(BaseResponseError)
InvalidToken = Class.new(BaseResponseError)
InvalidClient = Class.new(BaseResponseError)
Expand All @@ -60,6 +62,7 @@ def initialize(response)
UnsupportedResponseMode = Class.new(BaseResponseError)

AccessDenied = Class.new(BaseResponseError)
ServerError = Class.new(BaseResponseError)

TokenExpired = Class.new(InvalidToken)
TokenRevoked = Class.new(InvalidToken)
Expand Down
4 changes: 2 additions & 2 deletions lib/doorkeeper/oauth/error_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class ErrorResponse < BaseResponse
def self.from_request(request, attributes = {})
new(
attributes.merge(
name: request.error.to_s.demodulize.underscore.to_sym,
exception_class: request.error <= Errors::BaseResponseError ? request.error : nil,
name: request.error&.name_for_response,
exception_class: request.error,
state: request.try(:state),
redirect_uri: request.try(:redirect_uri),
),
Expand Down
8 changes: 4 additions & 4 deletions spec/controllers/authorizations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ def query_params
default_scopes_exist :public
end

it "does not redirect" do
it "raises InvalidScope error" do
expect do
get :new, params: {
client_id: client.uid,
Expand All @@ -1190,7 +1190,7 @@ def query_params
redirect_uri: client.redirect_uri,
state: "return-this",
}
end.to raise_error(Doorkeeper::Errors::BaseResponseError)
end.to raise_error(Doorkeeper::Errors::InvalidScope)
end
end

Expand All @@ -1199,14 +1199,14 @@ def query_params
default_scopes_exist :public
end

it "does not redirect" do
it "raises InvalidRedirectUri error" do
expect do
get :new, params: {
client_id: client.uid,
response_type: "token",
redirect_uri: "invalid",
}
end.to raise_error(Doorkeeper::Errors::BaseResponseError)
end.to raise_error(Doorkeeper::Errors::InvalidRedirectUri)
end
end
end
Expand Down

0 comments on commit bdf3d50

Please sign in to comment.