Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- `INVALID_BODY_FORMAT` JSON:API error

### Changed
- Rescue and translate `MultiJson::ParseError` while parsing body

## [2.1.0] - 2020-04-01
### Added
- Headers validation
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ with one issue per line.
| `:code` | `:status` | What is it? |
|:--------------------------|:----------|:------------|
| INVALID_RESOURCE_SCHEMA | 422 | Resource did not pass configured validation |
| INVALID_BODY_FORMAT | 400 | Request body is invalid JSON |
| INVALID_QUERY_PARAMETER | 400 | Query parameter violates syntax or did not pass configured validation |
| MISSING_QUERY_PARAMETER | 400 | Query parameter required in configuration is missing |
| INVALID_JSON_API | 400 | Request body violates JSON:API syntax |
Expand Down
4 changes: 4 additions & 0 deletions lib/request_handler/body_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def request_body
b.rewind
b = b.read
b.empty? ? {} : MultiJson.load(b)
rescue MultiJson::ParseError => e
raise BodyFormatError, [{ status: '400',
code: 'INVALID_BODY_FORMAT',
detail: e.message }]
end

attr_reader :request, :schema, :schema_options, :type
Expand Down
2 changes: 2 additions & 0 deletions lib/request_handler/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class OptionNotAllowedError < JsonApiError
class NoConfigAvailableError < InternalBaseError
end

class BodyFormatError < ExternalArgumentError
end
class BodyParamsError < ExternalArgumentError
end
class FieldsetsParamsError < ExternalArgumentError
Expand Down
14 changes: 14 additions & 0 deletions spec/request_handler/body_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@
.to raise_error(RequestHandler::MissingArgumentError)
end

it 'fails if the request body is not json' do
schema = Dry::Schema.JSON {}
expect do
described_class.new(
schema: schema,
type: type,
request: instance_double('Rack::Request',
params: {},
body: StringIO.new('foo'))
).run
end
.to raise_error(RequestHandler::BodyFormatError)
end

it "doesn't fail if the request body does not contain a data hash" do
schema = Dry::Schema.JSON {}
expect do
Expand Down