refactor: error handling return exception message

This commit is contained in:
Juan Rodriguez
2024-05-13 09:22:36 +02:00
parent 9564559610
commit b20417d579
7 changed files with 26 additions and 27 deletions
+10 -6
View File
@@ -6,18 +6,15 @@ TODO: Write a description here
```bash
brew tap amberframework/micrate
brew install micrate crystal
brew install micrate
shards install
```
TODO: Write installation instructions here
## Usage
```bash
crystal build url-shortener.cr --release --progress
ENV=production ./url-shortener
```
TODO: Write usage instructions here
## Development
@@ -27,6 +24,13 @@ DATABASE_URL=sqlite3://./sqlite/data.db micrate up
crystal run url-shortener.cr
```
## Build
```bash
crystal build url-shortener.cr --release --progress
ENV=production ./url-shortener
```
## Contributing
1. Fork it (<https://github.com/your-github-user/url-shortener/fork>)
+1 -1
View File
@@ -1,5 +1,5 @@
require "kemal"
Kemal.config.env = ENV["ENV"]? || "development"
Kemal.config.port = ENV["PORT"]?.try(&.to_i) || 3000
Kemal.config.port = ENV["PORT"]?.try(&.to_i) || 4000
Kemal.config.host_binding = ENV["HOST"]? || "0.0.0.0"
+7 -7
View File
@@ -10,10 +10,10 @@ module App::Controllers::Link
def call(env)
json_params = env.params.json.to_h
url = json_params.has_key?("url") ? json_params["url"] : nil
raise App::BadRequestException.new(env) if !url #TODO: return url "required field" error message
raise App::BadRequestException.new(env, {"url" => "Required field"}) if !url
link = Link.new
link.id = UUID.v4().to_s
link.id = UUID.v4.to_s
link.url = url.to_s
link.slug = Random::Secure.urlsafe_base64(4)
@@ -21,10 +21,10 @@ module App::Controllers::Link
if !changeset.valid?
errors = {"errors" => map_changeset_errors(changeset.errors)}
raise App::UnprocessableEntityException.new(env, errors.to_json)
raise App::UnprocessableEntityException.new(env, errors)
end
response = {"data" => App::Serializers::Link.new(link) }
response = {"data" => App::Serializers::Link.new(link)}
response.to_json
end
end
@@ -44,7 +44,7 @@ module App::Controllers::Link
changeset = Database.update(link)
if changeset.errors.any?
Log.error { "Increase click counter failed: #{changeset.errors}"}
Log.error { "Increase click counter failed: #{changeset.errors}" }
end
end
@@ -62,10 +62,10 @@ module App::Controllers::Link
link = Database.get(Link, id)
raise App::NotFoundException.new(env) if !link
response = {"data" => App::Serializers::Link.new(link) }
response = {"data" => App::Serializers::Link.new(link)}
response.to_json
end
end
#TODO: update, delete
# TODO: update, delete
end
+4 -4
View File
@@ -2,8 +2,9 @@ require "kemal"
module App
class BadRequestException < Kemal::Exceptions::CustomException
def initialize(context)
def initialize(context, message = Hash(String, String))
context.response.status_code = 400
context.response.print({ "error" => message }.to_json)
super(context)
end
end
@@ -16,11 +17,10 @@ module App
end
class UnprocessableEntityException < Kemal::Exceptions::CustomException
def initialize(context, @content = "")
def initialize(context, message = Hash(String, String))
context.response.status_code = 422
context.response.print({ "error" => message }.to_json)
super(context)
end
getter :content
end
end
+1 -1
View File
@@ -11,8 +11,8 @@ module App::Serializers
builder.object do
builder.field("id", @link.id)
builder.field("link", "#{ENV["APP_URL"]}/#{@link.slug}")
builder.field("clicks", @link.click_counter)
builder.field("origin", @link.url)
builder.field("clicks", @link.click_counter)
end
end
end
-4
View File
@@ -24,10 +24,6 @@ shards:
git: https://github.com/kemalcr/kemal.git
version: 1.5.0
micrate:
git: https://github.com/amberframework/micrate.git
version: 0.3.3
radix:
git: https://github.com/luislavena/radix.git
version: 0.4.1
+3 -4
View File
@@ -7,9 +7,8 @@ require "./app/serializers/*"
require "./app/routes"
error 500 { |env| {"status" => 500, "error" => "Internal Server Error"}.to_json }
error 401 { |env| {"status" => 401, "error" => "Unauthorized"}.to_json }
error 403 { |env| {"status" => 403, "error" => "Forbidden"}.to_json }
error 404 { |env| {"status" => 404, "error" => "Not Found"}.to_json }
error 500 { |env| {"error" => "Internal Server Error"}.to_json }
error 401 { |env| {"error" => "Unauthorized"}.to_json }
error 403 { |env| {"error" => "Forbidden"}.to_json }
Kemal.run