Skip to content

Commit 73d32b7

Browse files
adding exception handling to the UsersController show method
1 parent 14604af commit 73d32b7

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

app/controllers/users_controller.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ class UsersController < ApplicationController
1212

1313
# GET /users/1
1414
def show
15-
@user = User.find(params[:id].to_i)
16-
render json: @user
15+
begin
16+
@user = User.find(params[:id].to_i)
17+
render json: @user
18+
rescue ActiveRecord::RecordNotFound => e
19+
render json: {
20+
error: e.to_s
21+
}, status: :not_found
22+
end
1723
end
1824

1925
# POST /users V1

0 commit comments

Comments
 (0)