README.md in json_api_client-1.8.0 vs README.md in json_api_client-1.9.0

- old
+ new

@@ -472,10 +472,48 @@ # will use the customized connection end end ``` +##### Custom status handler + +You can change handling of response status using `connection_options`. For example you can override 400 status handling. +By default it raises `JsonApiClient::Errors::ClientError` but you can skip exception if you want to process errors from the server. +You need to provide a `proc` which should call `throw(:handled)` default handler for this status should be skipped. +```ruby +class ApiBadRequestHandler + def self.call(_env) + # do not raise exception + end +end + +class CustomUnauthorizedError < StandardError + attr_reader :env + + def initialize(env) + @env = env + super('not authorized') + end +end + +MyApi::Base.connection_options[:status_handlers] = { + 400 => ApiBadRequestHandler, + 401 => ->(env) { raise CustomUnauthorizedError, env } +} + +module MyApi + class User < Base + # will use the customized status_handlers + end +end + +user = MyApi::User.create(name: 'foo') +# server responds with { errors: [ { detail: 'bad request' } ] } +user.errors.messages # { base: ['bad request'] } +# on 401 it will raise CustomUnauthorizedError instead of JsonApiClient::Errors::NotAuthorized +``` + ##### Specifying an HTTP Proxy All resources have a class method ```connection_options``` used to pass options to the JsonApiClient::Connection initializer. ```ruby @@ -551,9 +589,25 @@ class MyApi::Base < JsonApiClient::Resource self.paginator = MyPaginator end ``` +### Custom type + +If your model must be named differently from classified type of resource you can easily customize it. +It will work both for defined and not defined relationships + +```ruby +class MyApi::Base < JsonApiClient::Resource + resolve_custom_type 'document--files', 'File' +end + +class MyApi::File < MyApi::Base + def self.resource_name + 'document--files' + end +end +``` ### Type Casting You can define your own types and its casting mechanism for schema.