Skip to content

Commit

Permalink
Better error message for invalid url format
Browse files Browse the repository at this point in the history
  • Loading branch information
scottwater committed Feb 1, 2019
1 parent d5a5f5c commit 09d70d9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/controllers/links_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def create
if link.save
render json: {id: link.share_id, url: link.share_url(base_share_url)}, status: :created
else
render json: {error: "Your link could not be created"}, status: :unauthorized
error = link.errors.any? ? link.errors.full_messages.join(", ") : "Unknown error"
render json: {error: error}, status: :unprocessable_entity
end
end

Expand Down
6 changes: 6 additions & 0 deletions spec/requests/links_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
expect(response).to have_http_status(:created)
end

it "will return unprocessable_entity if the link is not valid" do
post "/", params: {url: "scottw.com"}
expect(response).to have_http_status(:unprocessable_entity)
expect(json["error"]).to match(/Invalid Host/)
end

it "will add a new link with a custom URL" do
ENV["SHORTI_BASE_URL"] = "https://shorti.shorti/"
post "/", params: {url: "https://scottw.com"}
Expand Down

0 comments on commit 09d70d9

Please sign in to comment.