Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonne Haß <me@jhass.eu>2020-01-30 01:32:33 +0300
committerJonne Haß <me@jhass.eu>2020-02-02 20:04:11 +0300
commite8b9a70fbf7fa97d786733ee030e1fd0af2b6181 (patch)
tree1271d0756ea7381b7a8b6b12c432ab2f946e25d1 /app/controllers
parent9e762fcc317c79a144e0c308abf253a87dbfba2f (diff)
Ensure API responses adhere to documented error format
Diffstat (limited to 'app/controllers')
-rw-r--r--app/controllers/api/v1/aspects_controller.rb14
-rw-r--r--app/controllers/api/v1/base_controller.rb4
-rw-r--r--app/controllers/api/v1/comments_controller.rb14
-rw-r--r--app/controllers/api/v1/contacts_controller.rb10
-rw-r--r--app/controllers/api/v1/conversations_controller.rb4
-rw-r--r--app/controllers/api/v1/likes_controller.rb11
-rw-r--r--app/controllers/api/v1/messages_controller.rb4
-rw-r--r--app/controllers/api/v1/notifications_controller.rb10
-rw-r--r--app/controllers/api/v1/photos_controller.rb6
-rw-r--r--app/controllers/api/v1/post_interactions_controller.rb22
-rw-r--r--app/controllers/api/v1/posts_controller.rb6
-rw-r--r--app/controllers/api/v1/reshares_controller.rb6
-rw-r--r--app/controllers/api/v1/search_controller.rb2
-rwxr-xr-xapp/controllers/api/v1/tag_followings_controller.rb2
-rw-r--r--app/controllers/api/v1/users_controller.rb8
15 files changed, 64 insertions, 59 deletions
diff --git a/app/controllers/api/v1/aspects_controller.rb b/app/controllers/api/v1/aspects_controller.rb
index d18263847..a05f4cbae 100644
--- a/app/controllers/api/v1/aspects_controller.rb
+++ b/app/controllers/api/v1/aspects_controller.rb
@@ -23,7 +23,7 @@ module Api
if aspect
render json: aspect_as_json(aspect, true)
else
- render json: I18n.t("api.endpoint_errors.aspects.not_found"), status: :not_found
+ render_error 404, I18n.t("api.endpoint_errors.aspects.not_found")
end
end
@@ -33,24 +33,24 @@ module Api
if aspect&.save
render json: aspect_as_json(aspect, true)
else
- render json: I18n.t("api.endpoint_errors.aspects.cant_create"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.aspects.cant_create")
end
rescue ActionController::ParameterMissing
- render json: I18n.t("api.endpoint_errors.aspects.cant_create"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.aspects.cant_create")
end
def update
aspect = current_user.aspects.where(id: params[:id]).first
if !aspect
- render json: I18n.t("api.endpoint_errors.aspects.cant_update"), status: :not_found
+ render_error 404, I18n.t("api.endpoint_errors.aspects.cant_update")
elsif aspect.update!(aspect_params(true))
render json: aspect_as_json(aspect, true)
else
- render json: I18n.t("api.endpoint_errors.aspects.cant_update"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.aspects.cant_update")
end
rescue ActionController::ParameterMissing, ActiveRecord::RecordInvalid
- render json: I18n.t("api.endpoint_errors.aspects.cant_update"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.aspects.cant_update")
end
def destroy
@@ -58,7 +58,7 @@ module Api
if aspect&.destroy
head :no_content
else
- render json: I18n.t("api.endpoint_errors.aspects.cant_delete"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.aspects.cant_delete")
end
end
diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb
index e112b06f1..970af0729 100644
--- a/app/controllers/api/v1/base_controller.rb
+++ b/app/controllers/api/v1/base_controller.rb
@@ -58,6 +58,10 @@ module Api
render json: page[:data]
end
+ def render_error(code, message)
+ render json: {code: code, message: message}, status: code
+ end
+
def time_pager(query)
Api::Paging::RestPaginatorBuilder.new(query, request).time_pager(params)
end
diff --git a/app/controllers/api/v1/comments_controller.rb b/app/controllers/api/v1/comments_controller.rb
index be00f9e7e..405df9e8f 100644
--- a/app/controllers/api/v1/comments_controller.rb
+++ b/app/controllers/api/v1/comments_controller.rb
@@ -12,18 +12,18 @@ module Api
end
rescue_from ActiveRecord::RecordNotFound do
- render json: I18n.t("api.endpoint_errors.posts.post_not_found"), status: :not_found
+ render_error 404, I18n.t("api.endpoint_errors.posts.post_not_found")
end
rescue_from ActiveRecord::RecordInvalid do
- render json: I18n.t("api.endpoint_errors.comments.not_allowed"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.comments.not_allowed")
end
def create
find_post
comment = comment_service.create(params.require(:post_id), params.require(:body))
rescue ActiveRecord::RecordNotFound
- render json: I18n.t("api.endpoint_errors.posts.post_not_found"), status: :not_found
+ render_error 404, I18n.t("api.endpoint_errors.posts.post_not_found")
else
render json: comment_as_json(comment), status: :created
end
@@ -45,7 +45,7 @@ module Api
head :no_content
end
rescue ActiveRecord::RecordInvalid
- render json: I18n.t("api.endpoint_errors.comments.no_delete"), status: :forbidden
+ render_error 403, I18n.t("api.endpoint_errors.comments.no_delete")
end
def report
@@ -64,7 +64,7 @@ module Api
if report.save
head :no_content
else
- render json: I18n.t("api.endpoint_errors.comments.duplicate_report"), status: :conflict
+ render_error 409, I18n.t("api.endpoint_errors.comments.duplicate_report")
end
end
@@ -72,10 +72,10 @@ module Api
def comment_and_post_validate(post_guid, comment_guid)
if !comment_exists(comment_guid)
- render json: I18n.t("api.endpoint_errors.comments.not_found"), status: :not_found
+ render_error 404, I18n.t("api.endpoint_errors.comments.not_found")
false
elsif !comment_is_for_post(post_guid, comment_guid)
- render json: I18n.t("api.endpoint_errors.comments.not_found"), status: :not_found
+ render_error 404, I18n.t("api.endpoint_errors.comments.not_found")
false
else
true
diff --git a/app/controllers/api/v1/contacts_controller.rb b/app/controllers/api/v1/contacts_controller.rb
index 50760d0ea..41028204f 100644
--- a/app/controllers/api/v1/contacts_controller.rb
+++ b/app/controllers/api/v1/contacts_controller.rb
@@ -14,7 +14,7 @@ module Api
end
rescue_from ActiveRecord::RecordNotFound do
- render json: I18n.t("api.endpoint_errors.aspects.not_found"), status: :not_found
+ render_error 404, I18n.t("api.endpoint_errors.aspects.not_found")
end
def index
@@ -34,10 +34,10 @@ module Api
if aspect_membership
head :no_content
else
- render json: I18n.t("api.endpoint_errors.contacts.cant_create"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.contacts.cant_create")
end
rescue ActiveRecord::RecordNotUnique
- render json: I18n.t("api.endpoint_errors.contacts.cant_create"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.contacts.cant_create")
end
def destroy
@@ -48,10 +48,10 @@ module Api
if result && result[:success]
head :no_content
else
- render json: I18n.t("api.endpoint_errors.contacts.cant_delete"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.contacts.cant_delete")
end
rescue ActiveRecord::RecordNotFound
- render json: I18n.t("api.endpoint_errors.contacts.not_found"), status: :not_found
+ render_error 404, I18n.t("api.endpoint_errors.contacts.not_found")
end
def aspects_membership_service
diff --git a/app/controllers/api/v1/conversations_controller.rb b/app/controllers/api/v1/conversations_controller.rb
index 23bdcf210..61649013c 100644
--- a/app/controllers/api/v1/conversations_controller.rb
+++ b/app/controllers/api/v1/conversations_controller.rb
@@ -10,7 +10,7 @@ module Api
end
rescue_from ActiveRecord::RecordNotFound do
- render json: I18n.t("api.endpoint_errors.conversations.not_found"), status: :not_found
+ render_error 404, I18n.t("api.endpoint_errors.conversations.not_found")
end
def index
@@ -40,7 +40,7 @@ module Api
Diaspora::Federation::Dispatcher.defer_dispatch(current_user, conversation)
render json: conversation_as_json(conversation), status: :created
rescue ActiveRecord::RecordInvalid, ActionController::ParameterMissing, ActiveRecord::RecordNotFound
- render json: I18n.t("api.endpoint_errors.conversations.cant_process"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.conversations.cant_process")
end
def destroy
diff --git a/app/controllers/api/v1/likes_controller.rb b/app/controllers/api/v1/likes_controller.rb
index ba524c236..0b4180776 100644
--- a/app/controllers/api/v1/likes_controller.rb
+++ b/app/controllers/api/v1/likes_controller.rb
@@ -12,11 +12,11 @@ module Api
end
rescue_from ActiveRecord::RecordNotFound do
- render json: I18n.t("api.endpoint_errors.posts.post_not_found"), status: :not_found
+ render_error 404, I18n.t("api.endpoint_errors.posts.post_not_found")
end
rescue_from ActiveRecord::RecordInvalid do
- render json: I18n.t("api.endpoint_errors.likes.user_not_allowed_to_like"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.likes.user_not_allowed_to_like")
end
def show
@@ -35,8 +35,9 @@ module Api
like_service.create(params[:post_id])
rescue ActiveRecord::RecordInvalid => e
- return render json: I18n.t("api.endpoint_errors.likes.like_exists"), status: :unprocessable_entity if
- e.message == "Validation failed: Target has already been taken"
+ if e.message == "Validation failed: Target has already been taken"
+ return render_error 422, I18n.t("api.endpoint_errors.likes.like_exists")
+ end
raise
else
@@ -51,7 +52,7 @@ module Api
if success
head :no_content
else
- render json: I18n.t("api.endpoint_errors.likes.no_like"), status: :not_found
+ render_error 404, I18n.t("api.endpoint_errors.likes.no_like")
end
end
diff --git a/app/controllers/api/v1/messages_controller.rb b/app/controllers/api/v1/messages_controller.rb
index dc2839447..f6db1ba2a 100644
--- a/app/controllers/api/v1/messages_controller.rb
+++ b/app/controllers/api/v1/messages_controller.rb
@@ -8,7 +8,7 @@ module Api
end
rescue_from ActiveRecord::RecordNotFound do
- render json: I18n.t("api.endpoint_errors.conversations.not_found"), status: :not_found
+ render_error 404, I18n.t("api.endpoint_errors.conversations.not_found")
end
def create
@@ -19,7 +19,7 @@ module Api
Diaspora::Federation::Dispatcher.defer_dispatch(current_user, message)
render json: message_json(message), status: :created
rescue ActionController::ParameterMissing
- render json: I18n.t("api.endpoint_errors.conversations.cant_process"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.conversations.cant_process")
end
def index
diff --git a/app/controllers/api/v1/notifications_controller.rb b/app/controllers/api/v1/notifications_controller.rb
index a00063b4e..94a5b41a2 100644
--- a/app/controllers/api/v1/notifications_controller.rb
+++ b/app/controllers/api/v1/notifications_controller.rb
@@ -8,7 +8,7 @@ module Api
end
rescue_from ActiveRecord::RecordNotFound do
- render json: I18n.t("api.endpoint_errors.notifications.not_found"), status: :not_found
+ render_error 404, I18n.t("api.endpoint_errors.notifications.not_found")
end
def show
@@ -17,7 +17,7 @@ module Api
if notification
render json: NotificationPresenter.new(notification).as_api_json(true)
else
- render json: I18n.t("api.endpoint_errors.notifications.not_found"), status: :not_found
+ render_error 404, I18n.t("api.endpoint_errors.notifications.not_found")
end
end
@@ -31,7 +31,7 @@ module Api
end
render_paged_api_response notifications_page
rescue ArgumentError
- render json: I18n.t("api.endpoint_errors.notifications.cant_process"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.notifications.cant_process")
end
def update
@@ -39,10 +39,10 @@ module Api
if service.update_status_by_guid(params[:id], read)
head :no_content
else
- render json: I18n.t("api.endpoint_errors.notifications.cant_process"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.notifications.cant_process")
end
rescue ActionController::ParameterMissing
- render json: I18n.t("api.endpoint_errors.notifications.cant_process"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.notifications.cant_process")
end
private
diff --git a/app/controllers/api/v1/photos_controller.rb b/app/controllers/api/v1/photos_controller.rb
index d56012311..b97f12ed3 100644
--- a/app/controllers/api/v1/photos_controller.rb
+++ b/app/controllers/api/v1/photos_controller.rb
@@ -12,7 +12,7 @@ module Api
end
rescue_from ActiveRecord::RecordNotFound do
- render json: I18n.t("api.endpoint_errors.photos.not_found"), status: :not_found
+ render_error 404, I18n.t("api.endpoint_errors.photos.not_found")
end
def index
@@ -46,7 +46,7 @@ module Api
render json: photo_json(photo)
rescue CarrierWave::IntegrityError, ActionController::ParameterMissing, RuntimeError
- render json: I18n.t("api.endpoint_errors.photos.failed_create"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.photos.failed_create")
end
def destroy
@@ -58,7 +58,7 @@ module Api
if current_user.retract(photo)
head :no_content
else
- render json: I18n.t("api.endpoint_errors.photos.failed_delete"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.photos.failed_delete")
end
end
diff --git a/app/controllers/api/v1/post_interactions_controller.rb b/app/controllers/api/v1/post_interactions_controller.rb
index 14809ebb8..5fa9557c3 100644
--- a/app/controllers/api/v1/post_interactions_controller.rb
+++ b/app/controllers/api/v1/post_interactions_controller.rb
@@ -10,7 +10,7 @@ module Api
end
rescue_from ActiveRecord::RecordNotFound do
- render json: I18n.t("api.endpoint_errors.posts.post_not_found"), status: :not_found
+ render_error 404, I18n.t("api.endpoint_errors.posts.post_not_found")
end
def subscribe
@@ -18,7 +18,7 @@ module Api
current_user.participate!(post)
head :no_content
rescue ActiveRecord::RecordInvalid
- render json: I18n.t("api.endpoint_errors.interactions.cant_subscribe"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.interactions.cant_subscribe")
end
def hide
@@ -45,27 +45,27 @@ module Api
if report.save
head :no_content
else
- render json: I18n.t("api.endpoint_errors.posts.cant_report"), status: :conflict
+ render_error 409, I18n.t("api.endpoint_errors.posts.cant_report")
end
rescue ActionController::ParameterMissing
- render json: I18n.t("api.endpoint_errors.posts.cant_report"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.posts.cant_report")
end
def vote
+ post = find_post
begin
- post = find_post
+ poll_vote = poll_service.vote(post.id, params[:poll_answer_id])
rescue ActiveRecord::RecordNotFound
- render json: I18n.t("api.endpoint_errors.posts.post_not_found"), status: :not_found
- return
+ # This, but not the find_post above, should return a 422,
+ # we just keep poll_vote nil so it goes into the else below
end
- poll_vote = poll_service.vote(post.id, params[:poll_answer_id])
if poll_vote
head :no_content
else
- render json: I18n.t("api.endpoint_errors.interactions.cant_vote"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.interactions.cant_vote")
end
- rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotFound
- render json: I18n.t("api.endpoint_errors.interactions.cant_vote"), status: :unprocessable_entity
+ rescue ActiveRecord::RecordInvalid
+ render_error 422, I18n.t("api.endpoint_errors.interactions.cant_vote")
end
private
diff --git a/app/controllers/api/v1/posts_controller.rb b/app/controllers/api/v1/posts_controller.rb
index 27c05ebec..eb854368f 100644
--- a/app/controllers/api/v1/posts_controller.rb
+++ b/app/controllers/api/v1/posts_controller.rb
@@ -14,7 +14,7 @@ module Api
end
rescue_from ActiveRecord::RecordNotFound do
- render json: I18n.t("api.endpoint_errors.posts.post_not_found"), status: :not_found
+ render_error 404, I18n.t("api.endpoint_errors.posts.post_not_found")
end
def show
@@ -31,14 +31,14 @@ module Api
@status_message = creation_service.create(creation_params)
render json: PostPresenter.new(@status_message, current_user).as_api_response
rescue StandardError
- render json: I18n.t("api.endpoint_errors.posts.failed_create"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.posts.failed_create")
end
def destroy
post_service.destroy(params[:id], private_modify?)
head :no_content
rescue Diaspora::NotMine, Diaspora::NonPublic
- render json: I18n.t("api.endpoint_errors.posts.failed_delete"), status: :forbidden
+ render_error 403, I18n.t("api.endpoint_errors.posts.failed_delete")
end
private
diff --git a/app/controllers/api/v1/reshares_controller.rb b/app/controllers/api/v1/reshares_controller.rb
index 8caca9e7e..40e9fdf04 100644
--- a/app/controllers/api/v1/reshares_controller.rb
+++ b/app/controllers/api/v1/reshares_controller.rb
@@ -12,11 +12,11 @@ module Api
end
rescue_from ActiveRecord::RecordNotFound do
- render json: I18n.t("api.endpoint_errors.posts.post_not_found"), status: :not_found
+ render_error 404, I18n.t("api.endpoint_errors.posts.post_not_found")
end
rescue_from Diaspora::NonPublic do
- render json: I18n.t("api.endpoint_errors.posts.post_not_found"), status: :not_found
+ render_error 404, I18n.t("api.endpoint_errors.posts.post_not_found")
end
def show
@@ -35,7 +35,7 @@ module Api
def create
reshare = reshare_service.create(params.require(:post_id))
rescue ActiveRecord::RecordNotFound, ActiveRecord::RecordInvalid, RuntimeError
- render plain: I18n.t("reshares.create.error"), status: :unprocessable_entity
+ render_error 422, I18n.t("reshares.create.error")
else
render json: PostPresenter.new(reshare, current_user).as_api_response
end
diff --git a/app/controllers/api/v1/search_controller.rb b/app/controllers/api/v1/search_controller.rb
index 512e9dba4..555c04c7c 100644
--- a/app/controllers/api/v1/search_controller.rb
+++ b/app/controllers/api/v1/search_controller.rb
@@ -8,7 +8,7 @@ module Api
end
rescue_from ActionController::ParameterMissing, RuntimeError do
- render json: I18n.t("api.endpoint_errors.search.cant_process"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.search.cant_process")
end
def user_index
diff --git a/app/controllers/api/v1/tag_followings_controller.rb b/app/controllers/api/v1/tag_followings_controller.rb
index ab51c70fa..5579a32f6 100755
--- a/app/controllers/api/v1/tag_followings_controller.rb
+++ b/app/controllers/api/v1/tag_followings_controller.rb
@@ -19,7 +19,7 @@ module Api
tag_followings_service.create(params.require(:name))
head :no_content
rescue StandardError
- render json: I18n.t("api.endpoint_errors.tags.cant_process"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.tags.cant_process")
end
def destroy
diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb
index 576cc1fc6..64f3b77ba 100644
--- a/app/controllers/api/v1/users_controller.rb
+++ b/app/controllers/api/v1/users_controller.rb
@@ -22,7 +22,7 @@ module Api
end
rescue_from ActiveRecord::RecordNotFound do
- render json: I18n.t("api.endpoint_errors.users.not_found"), status: :not_found
+ render_error 404, I18n.t("api.endpoint_errors.users.not_found")
end
def show
@@ -42,15 +42,15 @@ module Api
if params_to_update && current_user.update_profile(params_to_update)
render json: PersonPresenter.new(current_user.person, current_user).profile_hash_as_api_json
else
- render json: I18n.t("api.endpoint_errors.users.cant_update"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.users.cant_update")
end
rescue RuntimeError
- render json: I18n.t("api.endpoint_errors.users.cant_update"), status: :unprocessable_entity
+ render_error 422, I18n.t("api.endpoint_errors.users.cant_update")
end
def contacts
if params.require(:user_id) != current_user.guid
- render json: I18n.t("api.endpoint_errors.users.not_found"), status: :not_found
+ render_error 404, I18n.t("api.endpoint_errors.users.not_found")
return
end