-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
144 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,7 +106,7 @@ product = Paddle::Product.retrieve(id: "pro_abc123") | |
# Update a product | ||
# https://developer.paddle.com/api-reference/products/update-product | ||
product.update(description: "This is a plan") | ||
# or | ||
# or | ||
Paddle::Product.update(id: "pro_abc123", description: "This is a plan") | ||
``` | ||
|
||
|
@@ -181,7 +181,7 @@ Paddle::Customer.list(email: "[email protected]") | |
|
||
# Create a customer | ||
# https://developer.paddle.com/api-reference/customers/create-customer | ||
# Returns a Paddle::ConflictError if the email is already used on Paddle | ||
# Returns a Paddle::Errors::ConflictError if the email is already used on Paddle | ||
Paddle::Customer.create(email: "[email protected]", name: "Customer Name") | ||
|
||
# Retrieve a customer | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module Paddle | ||
module Classic | ||
class Error < StandardError | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,127 +1,4 @@ | ||
module Paddle | ||
class Error < StandardError | ||
attr_reader :http_status_code | ||
attr_reader :paddle_error_code | ||
attr_reader :paddle_error_message | ||
|
||
def initialize(response_body, http_status_code) | ||
@response_body = response_body | ||
@http_status_code = http_status_code | ||
set_paddle_error_values | ||
super(build_message) | ||
end | ||
|
||
private | ||
|
||
def set_paddle_error_values | ||
@paddle_error_code = @response_body.dig("error", "code") | ||
@paddle_error_message = @response_body.dig("error", "detail") | ||
end | ||
|
||
def error_message | ||
@paddle_error_message || @response_body.dig("error", "code") | ||
rescue NoMethodError | ||
"An unknown error occurred." | ||
end | ||
|
||
def build_message | ||
if paddle_error_code.nil? | ||
return "Error #{@http_status_code}: #{error_message}" | ||
end | ||
"Error #{@http_status_code}: #{error_message} '#{paddle_error_code}'" | ||
end | ||
end | ||
|
||
class BadRequestError < Error | ||
private | ||
|
||
def error_message | ||
"Your request was malformed." | ||
end | ||
end | ||
|
||
class AuthenticationMissingError < Error | ||
private | ||
|
||
def error_message | ||
"You did not supply valid authentication credentials." | ||
end | ||
end | ||
|
||
class ForbiddenError < Error | ||
private | ||
|
||
def error_message | ||
"You are not allowed to perform that action." | ||
end | ||
end | ||
|
||
class EntityNotFoundError < Error | ||
private | ||
|
||
def error_message | ||
"No results were found for your request." | ||
end | ||
end | ||
|
||
class ConflictError < Error | ||
private | ||
|
||
def error_message | ||
"Your request was a conflict." | ||
end | ||
end | ||
|
||
class TooManyRequestsError < Error | ||
private | ||
|
||
def error_message | ||
"Your request exceeded the API rate limit." | ||
end | ||
end | ||
|
||
class InternalError < Error | ||
private | ||
|
||
def error_message | ||
"We were unable to perform the request due to server-side problems." | ||
end | ||
end | ||
|
||
class ServiceUnavailableError < Error | ||
private | ||
|
||
def error_message | ||
"You have been rate limited for sending more than 20 requests per second." | ||
end | ||
end | ||
|
||
class NotImplementedError < Error | ||
private | ||
|
||
def error_message | ||
"This resource has not been implemented." | ||
end | ||
end | ||
|
||
|
||
class ErrorFactory | ||
HTTP_ERROR_MAP = { | ||
400 => BadRequestError, | ||
401 => AuthenticationMissingError, | ||
403 => ForbiddenError, | ||
404 => EntityNotFoundError, | ||
409 => ConflictError, | ||
429 => TooManyRequestsError, | ||
500 => InternalError, | ||
503 => ServiceUnavailableError, | ||
501 => NotImplementedError | ||
}.freeze | ||
|
||
def self.create(response_body, http_status_code) | ||
status = http_status_code | ||
error_class = HTTP_ERROR_MAP[status] || Error | ||
error_class.new(response_body, http_status_code) if error_class | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
module Paddle | ||
class ErrorGenerator < StandardError | ||
attr_reader :http_status_code | ||
attr_reader :paddle_error_code | ||
attr_reader :paddle_error_message | ||
|
||
def initialize(response_body, http_status_code) | ||
@response_body = response_body | ||
@http_status_code = http_status_code | ||
set_paddle_error_values | ||
super(build_message) | ||
end | ||
|
||
private | ||
|
||
def set_paddle_error_values | ||
@paddle_error_code = @response_body.dig("error", "code") | ||
@paddle_error_message = @response_body.dig("error", "detail") | ||
end | ||
|
||
def error_message | ||
@paddle_error_message || @response_body.dig("error", "code") | ||
rescue NoMethodError | ||
"An unknown error occurred." | ||
end | ||
|
||
def build_message | ||
if paddle_error_code.nil? | ||
return "Error #{@http_status_code}: #{error_message}" | ||
end | ||
"Error #{@http_status_code}: #{error_message} '#{paddle_error_code}'" | ||
end | ||
end | ||
|
||
module Errors | ||
class BadRequestError < ErrorGenerator | ||
private | ||
|
||
def error_message | ||
"Your request was malformed." | ||
end | ||
end | ||
|
||
class AuthenticationMissingError < ErrorGenerator | ||
private | ||
|
||
def error_message | ||
"You did not supply valid authentication credentials." | ||
end | ||
end | ||
|
||
class ForbiddenError < ErrorGenerator | ||
private | ||
|
||
def error_message | ||
"You are not allowed to perform that action." | ||
end | ||
end | ||
|
||
class EntityNotFoundError < ErrorGenerator | ||
private | ||
|
||
def error_message | ||
"No results were found for your request." | ||
end | ||
end | ||
|
||
class ConflictError < ErrorGenerator | ||
private | ||
|
||
def error_message | ||
"Your request was a conflict." | ||
end | ||
end | ||
|
||
class TooManyRequestsError < ErrorGenerator | ||
private | ||
|
||
def error_message | ||
"Your request exceeded the API rate limit." | ||
end | ||
end | ||
|
||
class InternalError < ErrorGenerator | ||
private | ||
|
||
def error_message | ||
"We were unable to perform the request due to server-side problems." | ||
end | ||
end | ||
|
||
class ServiceUnavailableError < ErrorGenerator | ||
private | ||
|
||
def error_message | ||
"You have been rate limited for sending more than 20 requests per second." | ||
end | ||
end | ||
|
||
class NotImplementedError < ErrorGenerator | ||
private | ||
|
||
def error_message | ||
"This resource has not been implemented." | ||
end | ||
end | ||
end | ||
|
||
class ErrorFactory | ||
HTTP_ERROR_MAP = { | ||
400 => Errors::BadRequestError, | ||
401 => Errors::AuthenticationMissingError, | ||
403 => Errors::ForbiddenError, | ||
404 => Errors::EntityNotFoundError, | ||
409 => Errors::ConflictError, | ||
429 => Errors::TooManyRequestsError, | ||
500 => Errors::InternalError, | ||
503 => Errors::ServiceUnavailableError, | ||
501 => Errors::NotImplementedError | ||
}.freeze | ||
|
||
def self.create(response_body, http_status_code) | ||
status = http_status_code | ||
error_class = HTTP_ERROR_MAP[status] || ErrorGenerator | ||
error_class.new(response_body, http_status_code) if error_class | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters