Skip to content

Commit

Permalink
refactor api error
Browse files Browse the repository at this point in the history
  • Loading branch information
jasl committed Jan 31, 2025
1 parent eb069ab commit e698496
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 53 deletions.
16 changes: 16 additions & 0 deletions app/controllers/api/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,21 @@
module Api
class ApplicationController < ActionController::API
include Pagy::Backends::UuidCursor

module ErrorConstants
OK = "ok"
INVALID_JSON = "invalid_json"
EVENT_NOT_FOUND = "event_not_found"
RECIPIENT_NOT_FOUND = "recipient_not_found"
TOPIC_NOT_FOUND = "topic_not_found"
BAD_EVENT = "bad_event"
POST_TOO_FAST = "post_too_fast"
end

rescue_from JSON::ParserError do
render json: {
status: ErrorConstants::INVALID_JSON
}, status: :bad_request
end
end
end
24 changes: 8 additions & 16 deletions app/controllers/api/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ def show
@event = Event.find_by!(eid: params[:id])

render json: {
status: "ok",
status: ErrorConstants::OK,
event: @event.nip1_hash,
extra: {
metadata: {
topic: @event.topic,
session: @event.session,
latest: {
Expand All @@ -21,36 +21,28 @@ def show
}
rescue ActiveRecord::RecordNotFound => _ex
render json: {
status: "error",
error: {
message: "Event not found"
}
status: ErrorConstants::EVENT_NOT_FOUND
}, status: :not_found
end

def create
@event = Event.from_raw params.require(:event)
if @event.save
render json: {
status: "ok",
status: ErrorConstants::OK,
event: @event.nip1_hash
}
else
render json: {
status: "error",
status: ErrorConstants::BAD_EVENT,
error: {
message: "Event not saved",
data: @event.errors.full_messages
}
}, status: :unprocessable_content
end
rescue ActiveRecord::StaleObjectError
render json: {
status: "error",
error: {
message: "Event not saved",
data: "Too fast requests."
}
status: ErrorConstants::POST_TOO_FAST
}, status: :unprocessable_content
end

Expand All @@ -73,8 +65,8 @@ def batch_create
end

render json: {
status: "ok",
returns: returns.map(&:nip1_hash),
status: ErrorConstants::OK,
saved: returns.map(&:nip1_hash),
errored: errored
}
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Api
class HomeController < Api::ApplicationController
def index
render json: {
status: "ok"
status: ErrorConstants::OK
}
end
end
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/api/recipients/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ class ApplicationController < ::Api::ApplicationController
def set_recipient
if params[:recipient_id].blank?
render json: {
status: "error",
error: "Recipient not found"
status: ErrorConstants::RECIPIENT_NOT_FOUND
}, status: :not_found
end

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/recipients/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def index
)

render json: {
status: "ok",
status: ErrorConstants::OK,
events: @records.map(&:nip1_hash),
pagination: {
has_more: @pagy.has_more?
Expand All @@ -21,7 +21,7 @@ def latest
@event = Event.of_recipient(@recipient).order(id: :desc).first

render json: {
status: "ok",
status: ErrorConstants::OK,
event: @event&.nip1_hash
}
end
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/api/topics/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ class ApplicationController < ::Api::ApplicationController
def set_topic
if params[:topic_id].blank?
render json: {
status: "error",
error: "Topic not found"
status: ErrorConstants::TOPIC_NOT_FOUND
}, status: :not_found
end

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/topics/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def index
)

render json: {
status: "ok",
status: ErrorConstants::OK,
events: @records.map(&:nip1_hash),
pagination: {
has_more: @pagy.has_more?
Expand All @@ -21,7 +21,7 @@ def latest
@event = Event.of_topic(@topic).order(id: :desc).first

render json: {
status: "ok",
status: ErrorConstants::OK,
event: @event&.nip1_hash
}
end
Expand Down
Loading

0 comments on commit e698496

Please sign in to comment.