hensei-api/app/errors/crew_errors.rb
Justin Edmund 872b6fdb59 add crew specs and fix error handling
- add transactional fixtures to rails_helper for test isolation
- restructure crew errors to CrewErrors module for Zeitwerk
- add rescue_from for CrewErrors::CrewError in api_controller
- add model specs for Crew and CrewMembership (34 examples)
- add controller specs for crews and memberships (28 examples)
- add crew-related specs to User model (22 examples)
- add factories for crews and crew_memberships
2025-12-03 22:51:34 -08:00

119 lines
1.8 KiB
Ruby

# frozen_string_literal: true
module CrewErrors
# Base class for all crew-related errors
class CrewError < StandardError
def http_status
:unprocessable_entity
end
def code
self.class.name.demodulize.underscore
end
def to_hash
{
message: message,
code: code
}
end
end
class AlreadyInCrewError < CrewError
def http_status
:unprocessable_entity
end
def code
'already_in_crew'
end
def message
'You are already in a crew'
end
end
class CaptainCannotLeaveError < CrewError
def http_status
:unprocessable_entity
end
def code
'captain_cannot_leave'
end
def message
'Captain must transfer ownership before leaving'
end
end
class CannotRemoveCaptainError < CrewError
def http_status
:unprocessable_entity
end
def code
'cannot_remove_captain'
end
def message
'Cannot remove the captain from the crew'
end
end
class ViceCaptainLimitError < CrewError
def http_status
:unprocessable_entity
end
def code
'vice_captain_limit'
end
def message
'Crew can only have up to 3 vice captains'
end
end
class NotInCrewError < CrewError
def http_status
:unprocessable_entity
end
def code
'not_in_crew'
end
def message
'You are not in a crew'
end
end
class MemberNotFoundError < CrewError
def http_status
:not_found
end
def code
'member_not_found'
end
def message
'Member not found in this crew'
end
end
class CannotDemoteCaptainError < CrewError
def http_status
:unprocessable_entity
end
def code
'cannot_demote_captain'
end
def message
'Cannot demote the captain'
end
end
end