Skip to content

Commit

Permalink
Merge pull request #1691 from doorkeeper-gem/make-errors-backward-com…
Browse files Browse the repository at this point in the history
…patible

Make new errors backward compatible
  • Loading branch information
nbulaj committed Feb 14, 2024
2 parents 6633934 + 33c7afa commit 6f49a5c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ User-visible changes worth mentioning.
## main

- [#PR ID] Add your changelog here.
- [#1691] Make new Doorkeeper errors backward compatible with older extensions.

## 5.6.8

Expand Down
16 changes: 14 additions & 2 deletions lib/doorkeeper/oauth/error_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,26 @@ class ErrorResponse < BaseResponse
def self.from_request(request, attributes = {})
new(
attributes.merge(
name: request.error&.name_for_response,
exception_class: request.error,
name: error_name_for(request.error),
exception_class: exception_class_for(request.error),
state: request.try(:state),
redirect_uri: request.try(:redirect_uri),
),
)
end

def self.error_name_for(error)
error.respond_to?(:name_for_response) ? error.name_for_response : error
end

def self.exception_class_for(error)
return error if error.respond_to?(:name_for_response)

"Doorkeeper::Errors::#{error.to_s.classify}".safe_constantize
end

private_class_method :error_name_for, :exception_class_for

delegate :name, :description, :state, to: :@error

def initialize(attributes = {})
Expand Down
7 changes: 7 additions & 0 deletions spec/lib/oauth/error_response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
error = described_class.from_request double(error: Doorkeeper::Errors::InvalidClient, state: :hello)
expect(error.state).to eq(:hello)
end

it "supports old extensions" do
error = described_class.from_request double(error: :invalid_client)
expect(error.name).to eq(:invalid_client)

expect { error.raise_exception! }.to raise_error(Doorkeeper::Errors::InvalidClient)
end
end

it "ignores empty error values" do
Expand Down

0 comments on commit 6f49a5c

Please sign in to comment.