@@ -10,9 +10,9 @@ def call(context)
1010 # 200 range for error handling only for this case.
1111 @handler . call ( context ) . on_done ( 200 ..599 ) do |response |
1212 if !valid_response? ( context )
13- code , message , data = http_status_error ( context )
14- response . error = build_error ( context , code , message , data )
15- elsif ( 300 ..599 ) . cover? ( context . http_response . status_code )
13+ code , data = http_status_error ( context )
14+ response . error = build_error ( context , code , data )
15+ elsif ( 400 ..599 ) . cover? ( context . http_response . status_code )
1616 response . error = error ( context )
1717 end
1818 end
@@ -26,60 +26,52 @@ def valid_response?(context)
2626 req_header == resp_header
2727 end
2828
29- # TODO: Fix this
30- # This is not correct per protocol tests. Some headers will determine the error code.
31- # If the body is empty, there is still potentially an error code from the header, but
32- # we are making a generic http status error instead. In a new major version, we should
33- # always try to extract header, and during extraction, check headers and body.
3429 def error ( context )
3530 body = context . http_response . body . read
3631 if body . empty?
37- code , message , data = http_status_error ( context )
32+ code , data = http_status_error ( context )
3833 else
39- code , message , data = extract_error ( body , context )
34+ code , data = extract_error ( body , context )
4035 end
41- build_error ( context , code , message , data )
36+ build_error ( context , code , data )
4237 end
4338
4439 def extract_error ( body , context )
4540 data = CBOR . decode ( body )
46- type = data [ '__type' ]
47- code = error_code ( type , context )
48- message = data [ 'message' ]
49- data = parse_error_data ( context , body , type )
50- [ code , message , data ]
41+ code = error_code ( context , data )
42+ data = parse_error_data ( context , body , code )
43+ [ code , data ]
5144 rescue CBOR ::Error
52- [ http_status_error_code ( context ) , '' , Schema ::EmptyStructure . new ]
45+ [ http_status_error_code ( context ) , Schema ::EmptyStructure . new ]
5346 end
5447
5548 def parse_error_data ( context , body , code )
5649 data = Schema ::EmptyStructure . new
5750 context . operation . errors . each do |ref |
58- next unless ref . shape . id == code
51+ next unless ref . shape . name == code
5952
6053 data = context . config . cbor_codec . deserialize ( ref , body , ref . shape . type . new )
6154 end
6255 data
6356 end
6457
65- def error_code ( type , context )
66- return type . split ( '#' ) . last if type
67-
68- http_status_error_code ( context )
58+ def error_code ( context , data )
59+ code = data [ '__type' ]
60+ code ||= http_status_error_code ( context )
61+ code . split ( '#' ) . last . split ( '$' ) . first
6962 end
7063
71- def build_error ( context , code , message , data )
64+ def build_error ( context , code , data )
7265 errors_module = context . client . class . errors_module
73- errors_module . error_class ( code ) . new ( context , message , data )
66+ errors_module . error_class ( code ) . new ( context , data )
7467 end
7568
7669 def http_status_error ( context )
77- [ http_status_error_code ( context ) , '' , Schema ::EmptyStructure . new ]
70+ [ http_status_error_code ( context ) , Schema ::EmptyStructure . new ]
7871 end
7972
8073 def http_status_error_code ( context )
81- status_code = context . http_response . status_code
82- "HTTP#{ status_code } Error"
74+ "HTTP#{ context . http_response . status_code } Error"
8375 end
8476 end
8577 end
0 commit comments