Skip to content

Commit

Permalink
Add ProfessorAlreadyExistsError (#13)
Browse files Browse the repository at this point in the history
* add ProfessorAlreadyExistsError and rescue it in #find_or_create_professor

* refine rescue

* gst

* move rescue in #create_professor
  • Loading branch information
Eschults authored Oct 12, 2023
1 parent e969527 commit ca87338
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/edusign/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ def create_professor(first_name:, last_name:, email:)
dontSendCredentials: true
}
api :post, "/professor", payload.to_json
rescue Response::ProfessorAlreadyExistsError
# NOTE(Eschults): try again finding the professor
find_or_create_professor(first_name: first_name, last_name: last_name, email: email)
end

def find_or_create_professor(first_name:, last_name:, email:)
Expand Down Expand Up @@ -279,6 +282,7 @@ def api(http_method, path, body = {}, opts = {})
raise GatewayTimeoutError, request.message if request.code == 504

response = Response.new(JSON.parse(request.body, symbolize_names: true))
raise Response::ProfessorAlreadyExistsError, response.message if response.message == Response::ProfessorAlreadyExistsError::PROFESSOR_ALREADY_EXISTS_MESSAGE
raise Response::Error, response.message if response.error?

response
Expand All @@ -296,6 +300,10 @@ class Error < StandardError
CANNOT_GET_PROFESSOR_BY_EMAIL_ERROR_MESSAGE = "Error - cannot get professor by email".freeze
end

class ProfessorAlreadyExistsError
PROFESSOR_ALREADY_EXISTS_MESSAGE = "Professor already exists in this school".freeze
end

attr_reader :body

def initialize(body)
Expand Down

0 comments on commit ca87338

Please sign in to comment.