Skip to content

Commit

Permalink
Merge pull request #202 from yoarslan/CU-8669bawf3_Make-error-respons…
Browse files Browse the repository at this point in the history
…e-consistent

CU-8669bawf3 | Make-error-response-consistent
  • Loading branch information
deveu-s authored Jan 20, 2023
2 parents 34f1ecb + 5b7f19a commit e9fa8b6
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 94 deletions.
8 changes: 4 additions & 4 deletions app/controllers/api/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Api::ApiController < ApplicationController

def set_workspace_in_session
if session[:current_workspace_id].nil?
render json: { message: 'No Workspace Selected.', status: :unprocessable_entity }
render json: { error: 'No Workspace Selected.' }, status: :unprocessable_entity
else
Current.workspace = Workspace.find_by(id: session[:current_workspace_id])
end
Expand All @@ -24,17 +24,17 @@ def set_profile
def presence_of_api_token
return unless request.headers['Authorization'].nil?

render json: { message: 'You need to sign in before to access this.' }, status: :unauthorized
render json: { error: 'You need to sign in before to access this.' }, status: :unauthorized
end

def authenticate_api_with_token
token = request.headers['Authorization'].split[1]
jwt_payload = JWT.decode(token, Rails.application.credentials.fetch(:secret_key_base))
@current_user = User.find(jwt_payload[0]['sub'])

render json: { message: 'You need to sign in before to access this.' }, status: :unauthorized unless current_user
render json: { error: 'You need to sign in before to access this.' }, status: :unauthorized unless current_user
Current.user = @current_user
rescue StandardError
render json: { message: 'You need to sign in before to access this.' }, status: :unauthorized
render json: { error: 'You need to sign in before to access this.' }, status: :unauthorized
end
end
27 changes: 13 additions & 14 deletions app/controllers/api/v1/bench_channels_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,32 @@ def create
if @bench_channel.save
create_first_bench_channel_participant
else
render json: { status: false, message: 'There was an error creating the channel.', errors: @bench_channel.errors }
render json: { error: 'There was an error creating the channel.', errors: @bench_channel.errors }, status: :unprocessable_entity
end
end
end

def update
return if @bench_channel.update(bench_channel_params)

render json: { message: 'Error while updating!', errors: @bench_channel.errors }, status: :unprocessable_entity
render json: { error: 'Error while updating!', errors: @bench_channel.errors }, status: :unprocessable_entity
end

def destroy
render json: if @bench_channel.destroy
{ message: 'Channel was successfully deleted.' }
else
{ message: @bench_channel.errors,
status: :unprocessable_entity }
end
if @bench_channel.destroy
render json: { message: 'Channel was successfully deleted.' }, status: :ok
else
render json: { error: 'Error while deleting', errors: @bench_channel.errors }, status: :unprocessable_entity
end
end

def leave_channel
ActiveRecord::Base.transaction do
if @channel_participant.destroy
InfoMessagesCreatorService.new(@bench_channel.bench_conversation.id).left_channel(@bench_channel.name)
render json: { status: :ok }
render json: { message: 'Channel left ' }, status: :ok
else
render json: { message: "Unable to leave ##{@bench_channel.name}." }, status: :unprocessable_entity
render json: { error: "Unable to leave ##{@bench_channel.name}." }, status: :unprocessable_entity
end
end
end
Expand All @@ -75,26 +74,26 @@ def set_bench_channel
@bench_channel = BenchChannel.includes(:profiles).find(params[:id])
return if !@bench_channel.is_private || Current.profile.bench_channel_ids.include?(@bench_channel.id)

render json: { errors: 'User is not part of channel.' }, status: :not_found
render json: { error: 'User is not part of channel.' }, status: :not_found
end

def set_channel_participant
@channel_participant = Current.profile.channel_participants.find_by(bench_channel_id: @bench_channel.id)

render json: { message: "You are not a member of ##{@bench_channel.name}." }, status: :not_found if @channel_participant.nil?
render json: { error: "You are not a member of ##{@bench_channel.name}." }, status: :not_found if @channel_participant.nil?
end

def set_left_on
@channel_participant.left_on = DateTime.current

return if @channel_participant.save

render json: { message: 'There was an error.', errors: @channel_participant.errors }, status: :unprocessable_entity
render json: { error: 'There was an error leaving channel.', errors: @channel_participant.errors }, status: :unprocessable_entity
end

def bench_channel_cannot_be_public_again
return unless @bench_channel.is_private? && !params[:bench_channel][:is_private]

render json: { message: "You cannot change ##{@bench_channel.name} to public again." }, status: :unprocessable_entity
render json: { error: "You cannot change ##{@bench_channel.name} to public again." }, status: :bad_request
end
end
6 changes: 3 additions & 3 deletions app/controllers/api/v1/bookmarks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ def create
if @bookmark.save
render json: { message: 'Bookmark is successfully created' }, status: :ok
else
render json: { errors: @bookmark.errors }, status: :unprocessable_entity
render json: { error: 'Unable to create bookmark', errors: @bookmark.errors }, status: :unprocessable_entity
end
end

def update
if @bookmark.update(bookmark_params)
render json: { message: 'Bookmark is successfully updated' }, status: :ok
else
render json: { error: @bookmark.errors }, status: :unprocessable_entity
render json: { error: 'Unable to update bookmark', errors: @bookmark.errors }, status: :unprocessable_entity
end
end

def destroy
if @bookmark.destroy
render json: { message: 'Bookmark is successfully removed' }, status: :ok
else
render json: { error: @bookmark.errors }, status: :unprocessable_entity
render json: { error: 'Unable to update bookmark', errors: @bookmark.errors }, status: :unprocessable_entity
end
end

Expand Down
16 changes: 8 additions & 8 deletions app/controllers/api/v1/channel_participants_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def create
end

InfoMessagesCreatorService.new(@bench_channel.bench_conversation.id).add_members_in_channel(@users_joined, params[:profile_ids][0])
render json: { status: :ok, member_count: @users_joined.count }
render json: { member_count: @users_joined.count }, status: :ok
end
end

Expand All @@ -34,9 +34,9 @@ def join_public_channel
ActiveRecord::Base.transaction do
if @channel_participant.save
InfoMessagesCreatorService.new(@bench_channel.bench_conversation.id).join_public_channel
render json: { status: :ok }
render json: { message: "Joined ##{@bench_channel.name}." }, status: :ok
else
render json: { errors: @channel_participant.errors, status: :unprocessable_entity }
render json: { error: "Unable to join ##{@bench_channel.name}.", errors: @channel_participant.errors }, status: :unprocessable_entity
end
end
end
Expand All @@ -53,29 +53,29 @@ def set_bench_channel
def check_workspace
return if Current.profile.workspace.eql?(@bench_channel.workspace)

render json: { error: 'This Channel is not part of your workspace.', status: :unprocessable_entity }
render json: { error: 'This Channel is not part of your workspace.' }, status: :forbidden
end

def check_already_joined_channel
is_channel_participant = @bench_channel.profile_ids.include?(Current.profile.id)

render json: { error: 'Already part of this channel.', status: :unprocessable_entity } if is_channel_participant
render json: { error: 'User already part of this channel.' }, status: :unprocessable_entity if is_channel_participant
end

def check_private_channel
return render json: { error: 'You cannot join Private Channel yourself.', status: :unprocessable_entity } if @bench_channel.is_private?
return render json: { error: 'You cannot join Private Channel yourself.' }, status: :forbidden if @bench_channel.is_private?
end

def check_channel_participants
@channel_members = ChannelParticipant.where(profile_id: params[:profile_ids], bench_channel_id: @bench_channel.id).ids
return render json: { error: 'One or Many Users already participant of this channel', status: :unprocessable_entity } if @channel_members.present?
return render json: { error: 'One or Many Users already participant of this channel' }, status: :forbidden if @channel_members.present?

@users_joined = Profile.where(id: params[:profile_ids]).pluck(:username)
end

def check_profile_ids
return if (params[:profile_ids] - Current.workspace.profile_ids).blank?

render json: { error: 'Profiles cannot be found', status: :unprocessable_entity }
render json: { error: 'Profiles cannot be found' }, status: :not_found
end
end
40 changes: 22 additions & 18 deletions app/controllers/api/v1/conversation_messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,21 @@ def send_message
def create
@message = ConversationMessage.new(conversation_messages_params)
@message.bench_conversation_id = @bench_conversation.id
response = if @message.save
{ message: 'Message sent.' }
else
{ message: @message.errors, status: :unprocessable_entity }
end
render json: response
render json: @message.save ? { message: 'Message sent.' } : { error: 'Message not sent', errors: @message.errors },
status: @message.save ? :ok : :unprocessable_entity
end

def update
if @message.update(conversation_messages_params)
render json: { success: 'Messages updated', message: @message, status: :updated }
render json: { success: 'Message updated', message: @message }, status: :ok
else
render json: { errors: @message.errors, status: :unprocessable_entity }
render json: { error: 'Message not updated', errors: @message.errors }, status: :unprocessable_entity
end
end

def destroy
render json: @message.destroy ? { message: 'Message deleted successfully.' } : { message: @message.errors, status: :unprocessable_entity }
render json: @message.destroy ? { message: 'Message deleted successfully.' } : { error: 'Message not deleted', errors: @message.errors },
status: @message.destroy ? :ok : :unprocessable_entity
end

def index_saved_messages
Expand All @@ -44,11 +41,19 @@ def index_saved_messages
def save_message
@saved_item = Current.profile.saved_items.new(conversation_message_id: params[:id])

render json: @saved_item.save ? { json: 'added to saved items' } : @saved_item.errors
if @saved_item.save
render json: { message: 'Added to saved items' }, status: :ok
else
render json: { error: 'Added to saved items', errors: @saved_item.errors }, status: :unprocessable_entity
end
end

def unsave_message
render json: @saved_item.destroy ? { json: 'Removed from saved items' } : @saved_item.errors
if @saved_item.destroy
render json: { message: 'Removed from saved items' }, status: :ok
else
render json: { error: 'Unable to remove from saved items', errors: @saved_item.errors }, status: :unprocessable_entity
end
end

def recent_files
Expand Down Expand Up @@ -85,7 +90,7 @@ def unread_messages
def paginate_messages
@pagy, @messages = pagination_for_chat_messages(@conversation.id, params[:page])

return render json: { errors: 'Page not Found.', status: :unprocessable_entity } if @pagy.nil?
return render json: { error: 'Page not Found.' }, status: :not_found if @pagy.nil?
end

def set_saved_item
Expand Down Expand Up @@ -115,32 +120,31 @@ def fetch_conversation
when 'profiles'
BenchConversation.profile_to_profile_conversation(Current.profile.id, conversation_id)
end

render json: { message: 'wrong type', status: :unprocessable_entity } if @bench_conversation.blank?
render json: { error: 'wrong type' }, status: :bad_request if @bench_conversation.blank?
end

def set_bench_channel
@bench_channel = BenchChannel.find(params[:id])
return if !@bench_channel.is_private || Current.profile.bench_channel_ids.include?(@bench_channel.id)

render json: { errors: 'User is not part of this channel' }, status: :not_found
render json: { error: 'User is not part of this channel' }, status: :not_found
end

def set_group
@group = Group.find(params[:id])
render json: { errors: 'User is not part of this group' }, status: :not_found unless @group.profile_ids.include?(Current.profile.id)
render json: { error: 'User is not part of this group' }, status: :not_found unless @group.profile_ids.include?(Current.profile.id)
end

def set_receiver
@receiver = Profile.find(params[:id])
render json: { message: "You can't access this profile.", status: :unprocessable_entity } unless @receiver.workspace_id.eql?(Current.workspace.id)
render json: { error: "You can't access this profile." }, status: :unprocessable_entity unless @receiver.workspace_id.eql?(Current.workspace.id)
end

def authenticat_message
if @message.sender_id.eql?(Current.profile.id)
check_membership(@message.bench_conversation)
else
render json: { message: 'Sorry, this message is not your', status: :unprocessable_entity }
render json: { error: 'Sorry, this message is not yours.' }, status: :unauthorized
end
end

Expand Down
12 changes: 6 additions & 6 deletions app/controllers/api/v1/downloads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ def index; end
def create
@download = Current.profile.downloads.new(download_params)
if @download.save
render json: @download, status: :ok
render json: @download, status: :created
else
render json: @download.errors, status: :unprocessable_entity
render json: { error: 'Unable to download file', errors: @download.errors }, status: :unprocessable_entity
end
end

def destroy
if @download.destroy
render json: 'download removed', status: :ok
render json: { message: 'Download removed' }, status: :ok
else
render json: @download.errors, status: :unprocessable_entity
render json: { error: 'Unable to remove download', errors: @download.errors }, status: :unprocessable_entity
end
end

def clear_all
if @downloads.destroy_all
render json: 'downloads removed', status: :ok
render json: { message: 'Downloads removed' }, status: :ok
else
render json: @downloads.errors, status: :unprocessable_entity
render json: { error: 'Unable to remove downloads', errors: @downloads.errors }, status: :unprocessable_entity
end
end

Expand Down
14 changes: 9 additions & 5 deletions app/controllers/api/v1/draft_messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,26 @@ def create
@draft_message = DraftMessage.new(draft_messages_params)

if @draft_message.save
render json: { success: 'Draft Message successfully created', draft_message: @draft_message, status: :created }
render json: { message: 'Draft Message successfully created', draft_message: @draft_message }, status: :ok
else
render json: { error: @draft_message.errors, status: :unprocessable_entity }
render json: { error: 'Unable to create draft message', errors: @draft_message.errors }, status: :unprocessable_entity
end
end

def update
if @draft_message.update(draft_messages_params)
render json: { success: 'Draft updated', draft_message: @draft_message, status: :updated }
render json: { message: 'Draft updated', draft_message: @draft_message }, status: :updated
else
render json: { errors: @draft_message.errors, status: :unprocessable_entity }
render json: { error: 'Draft not updated', errors: @draft_message.errors }, status: :unprocessable_entity
end
end

def destroy
render json: @draft_message.destroy ? { message: 'Draft deleted successfully.' } : { error: @draft_message.errors, status: :unprocessable_entity }
if @draft_message.destroy
render json: { message: 'Draft deleted successfully.' }, status: :ok
else
render json: { error: 'Draft not deleted', errors: @draft_message.errors }, status: :unprocessable_entity
end
end

private
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/api/v1/favourites_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ class Api::V1::FavouritesController < Api::ApiController
def create
@favourite = Favourite.find_or_create_by(favourites_params)
if @favourite.id
render json: { success: 'Channel successfully added to favourites', favourite: @favourite, status: :created }
render json: { message: 'Channel successfully added to favourites', favourite: @favourite }, status: :ok
else
render json: { error: @favourite.errors, status: :unprocessable_entity }
render json: { error: 'Channel not added to favourites', errors: @favourite.errors }, status: :unprocessable_entity
end
end

def destroy
if @favourite.destroy
render json: { success: 'Channel successfully removed from favourites', status: :success }
render json: { message: 'Channel successfully removed from favourites' }, status: :ok
else
render json: { error: favourite.errors, status: :unprocessable_entity }
render json: { error: 'Channel not removed to favourites', errors: @favourite.errors }, status: :unprocessable_entity
end
end

Expand All @@ -28,6 +28,6 @@ def favourites_params

def set_favourite
@favourite = Favourite.find_by(id: params[:id])
render json: { error: 'Channel could not be found in favourites', status: :unprocessable_entity } if @favourite.nil?
render json: { error: 'Channel could not be found in favourites' }, status: :not_found if @favourite.nil?
end
end
Loading

0 comments on commit e9fa8b6

Please sign in to comment.