Skip to content

Commit e698496

Browse files
committed
refactor api error
1 parent eb069ab commit e698496

File tree

8 files changed

+67
-53
lines changed

8 files changed

+67
-53
lines changed

app/controllers/api/application_controller.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,21 @@
33
module Api
44
class ApplicationController < ActionController::API
55
include Pagy::Backends::UuidCursor
6+
7+
module ErrorConstants
8+
OK = "ok"
9+
INVALID_JSON = "invalid_json"
10+
EVENT_NOT_FOUND = "event_not_found"
11+
RECIPIENT_NOT_FOUND = "recipient_not_found"
12+
TOPIC_NOT_FOUND = "topic_not_found"
13+
BAD_EVENT = "bad_event"
14+
POST_TOO_FAST = "post_too_fast"
15+
end
16+
17+
rescue_from JSON::ParserError do
18+
render json: {
19+
status: ErrorConstants::INVALID_JSON
20+
}, status: :bad_request
21+
end
622
end
723
end

app/controllers/api/events_controller.rb

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ def show
66
@event = Event.find_by!(eid: params[:id])
77

88
render json: {
9-
status: "ok",
9+
status: ErrorConstants::OK,
1010
event: @event.nip1_hash,
11-
extra: {
11+
metadata: {
1212
topic: @event.topic,
1313
session: @event.session,
1414
latest: {
@@ -21,36 +21,28 @@ def show
2121
}
2222
rescue ActiveRecord::RecordNotFound => _ex
2323
render json: {
24-
status: "error",
25-
error: {
26-
message: "Event not found"
27-
}
24+
status: ErrorConstants::EVENT_NOT_FOUND
2825
}, status: :not_found
2926
end
3027

3128
def create
3229
@event = Event.from_raw params.require(:event)
3330
if @event.save
3431
render json: {
35-
status: "ok",
32+
status: ErrorConstants::OK,
3633
event: @event.nip1_hash
3734
}
3835
else
3936
render json: {
40-
status: "error",
37+
status: ErrorConstants::BAD_EVENT,
4138
error: {
42-
message: "Event not saved",
4339
data: @event.errors.full_messages
4440
}
4541
}, status: :unprocessable_content
4642
end
4743
rescue ActiveRecord::StaleObjectError
4844
render json: {
49-
status: "error",
50-
error: {
51-
message: "Event not saved",
52-
data: "Too fast requests."
53-
}
45+
status: ErrorConstants::POST_TOO_FAST
5446
}, status: :unprocessable_content
5547
end
5648

@@ -73,8 +65,8 @@ def batch_create
7365
end
7466

7567
render json: {
76-
status: "ok",
77-
returns: returns.map(&:nip1_hash),
68+
status: ErrorConstants::OK,
69+
saved: returns.map(&:nip1_hash),
7870
errored: errored
7971
}
8072
end

app/controllers/api/home_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Api
44
class HomeController < Api::ApplicationController
55
def index
66
render json: {
7-
status: "ok"
7+
status: ErrorConstants::OK
88
}
99
end
1010
end

app/controllers/api/recipients/application_controller.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ class ApplicationController < ::Api::ApplicationController
1010
def set_recipient
1111
if params[:recipient_id].blank?
1212
render json: {
13-
status: "error",
14-
error: "Recipient not found"
13+
status: ErrorConstants::RECIPIENT_NOT_FOUND
1514
}, status: :not_found
1615
end
1716

app/controllers/api/recipients/events_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def index
99
)
1010

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

2323
render json: {
24-
status: "ok",
24+
status: ErrorConstants::OK,
2525
event: @event&.nip1_hash
2626
}
2727
end

app/controllers/api/topics/application_controller.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ class ApplicationController < ::Api::ApplicationController
1010
def set_topic
1111
if params[:topic_id].blank?
1212
render json: {
13-
status: "error",
14-
error: "Topic not found"
13+
status: ErrorConstants::TOPIC_NOT_FOUND
1514
}, status: :not_found
1615
end
1716

app/controllers/api/topics/events_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def index
99
)
1010

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

2323
render json: {
24-
status: "ok",
24+
status: ErrorConstants::OK,
2525
event: @event&.nip1_hash
2626
}
2727
end

0 commit comments

Comments
 (0)