Skip to content

Commit

Permalink
Merge pull request #9 from aristotelesbr/development
Browse files Browse the repository at this point in the history
Release 0.1.2: Enhance Content-Type Error Handling and Remove Debug Dependency
  • Loading branch information
aristotelesbr authored Nov 21, 2023
2 parents db6e300 + c20da75 commit d269f41
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 25 deletions.
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,28 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Released]
## [0.1.2] - 2023-23-11

### Added

- Implemented a specific error handler for Content-Type related errors, enhancing the system's ability to respond appropriately based on whether the request Content-Type is JSON or HTML.

### Removed

- Removed the debug gem from development dependencies, streamlining the development environment setup.

### Fixed

- Fixed a bug that prevented the correct reading of the Content-Type header in requests, ensuring proper handling of content types.

## [0.1.1] - 2023-23-11

### Added

- Introduced `Array.wrap` extension to the `Array` class for more reliable conversion of objects to arrays within the Lennarb router environment. This method ensures consistent array wrapping of single objects and `nil` values.

### Changed

- Refactored the `put_header` method to use the `Array.wrap` method for more predictable header value handling.
- Renamed methods to have a consistent `assign_` prefix to standardize the API interface:
- `put_header` to `assign_header`
Expand Down
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ gem 'rack', '~> 3.0', '>= 3.0.8'
gem 'colorize', '~> 1.1'

group :development, :test do
gem 'debug'
# [https://rubygems.org/gems/rake]
# Rake is a Make-like program implemented in Ruby. Tasks and dependencies are
# specified in standard Ruby syntax.
Expand Down
19 changes: 2 additions & 17 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
lennarb (0.1.1)
lennarb (0.1.2)
colorize (~> 1.1)
puma (~> 6.4)
rack (~> 3.0, >= 3.0.8)
Expand All @@ -12,34 +12,21 @@ GEM
specs:
ast (2.4.2)
colorize (1.1.0)
debug (1.8.0)
irb (>= 1.5.0)
reline (>= 0.3.1)
io-console (0.6.0)
irb (1.9.0)
rdoc
reline (>= 0.3.8)
json (2.6.3)
language_server-protocol (3.17.0.3)
minitest (5.20.0)
nio4r (2.5.9)
nio4r (2.6.1)
parallel (1.23.0)
parser (3.2.2.4)
ast (~> 2.4.1)
racc
psych (5.1.1.1)
stringio
puma (6.4.0)
nio4r (~> 2.0)
racc (1.7.3)
rack (3.0.8)
rainbow (3.1.1)
rake (13.1.0)
rdoc (6.6.0)
psych (>= 4.0.0)
regexp_parser (2.8.2)
reline (0.4.0)
io-console (~> 0.5)
rexml (3.2.6)
rubocop (1.57.2)
json (~> 2.3)
Expand All @@ -57,15 +44,13 @@ GEM
rubocop-minitest (0.33.0)
rubocop (>= 1.39, < 2.0)
ruby-progressbar (1.13.0)
stringio (3.0.9)
unicode-display_width (2.5.0)

PLATFORMS
x86_64-linux

DEPENDENCIES
colorize (~> 1.1)
debug
lennarb!
minitest (~> 5.20)
puma (~> 6.4)
Expand Down
43 changes: 38 additions & 5 deletions lib/lenna/middleware/default/error_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def call(req, res, next_middleware)
env = req.env
log_error(env, e)

render_error_page(e, env, res)
render_error_page(e, env, req, res)
end

private
Expand All @@ -37,10 +37,43 @@ def call(req, res, next_middleware)
# @return [void]
#
# @api private
def render_error_page(error, env, res)
res.put_status(500)
res.put_header('Content-Type', 'text/html')
res.put_body(error_page(error, env))
def render_error_page(error, env, req, res)
case req.headers['Content-Type']
in 'application/json' then render_json(error, env, res)
else render_html(error, env, res)
end
end

# This method will render the JSON error page.
#
# @param error [StandardError] The error object
# @param env [Hash] The environment variables
# @param res [Rack::Response] The response object
# @return [void]
#
# @api private
def render_json(error, env, res)
case env['RACK_ENV']
in 'development' | 'test' then res.json(
data: {
error: error.message,
status: 500
}
)
else res.json(error: 'Internal Server Error', status: 500)
end
end

# This method will render the HTML error page.
#
# @param error [StandardError] The error object
# @param env [Hash] The environment variables
# @param res [Rack::Response] The response object
# @return [void]
#
# @api private
def render_html(error, env, res)
res.html(error_page(error, env), status: 500)
end

# This method will log the error.
Expand Down
4 changes: 4 additions & 0 deletions lib/lenna/router/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ def body_content
# Turn this:
# HTTP_FOO=bar Foo=bar
def headers
content_type = env['CONTENT_TYPE']
@headers ||= env.select { |k, _| k.start_with?('HTTP_') }
.transform_keys { |k| format_header_name(k) }

@headers['Content-Type'] = content_type if content_type
@headers
end

# This method returns the request body in a normalized way.
Expand Down
2 changes: 1 addition & 1 deletion lib/lennarb/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Lennarb
VERSION = '0.1.1'
VERSION = '0.1.2'

public_constant :VERSION
end

0 comments on commit d269f41

Please sign in to comment.