Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.

Commit 3e29912

Browse files
committed
Create error code lookup function
Reduce file name from error_code_registry to errors. Use lookup function to access the error codes and throw a ValueError in case the error could not be found. Add all error hexa codes to the registry. The additional data is not replacing the error description, so remove test covering that incorrect case.
1 parent a553396 commit 3e29912

File tree

2 files changed

+43
-21
lines changed

2 files changed

+43
-21
lines changed

hyper/http20/connection.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
from .response import HTTP20Response, HTTP20Push
2020
from .window import FlowControlManager
2121
from .exceptions import ConnectionError
22-
from .error_code_registry import H2_ERROR_CODE_REGISTRY
2322

23+
import hyper.http20.errors as errors
2424
import errno
2525
import logging
2626
import socket
@@ -379,21 +379,17 @@ def receive_frame(self, frame):
379379
# If an error occured, try to read the error description from
380380
# code registry otherwise use the frame's additional data.
381381
if frame.error_code != 0:
382-
err_no = hex(frame.error_code)
383-
384-
if frame.error_code > 0 and \
385-
frame.error_code < len(H2_ERROR_CODE_REGISTRY):
386-
err_name = H2_ERROR_CODE_REGISTRY[frame.error_code]['Name']
387-
err_description = frame.additional_data or \
388-
H2_ERROR_CODE_REGISTRY[frame.error_code]['Description']
382+
try:
383+
name, number, description = errors.get_data(frame.error_code)
384+
except ValueError:
385+
error_string = ("Encountered error code %d, extra data %s" %
386+
(frame.error_code, frame.additional_data))
389387
else:
390-
err_name = frame.error_code;
391-
err_description = frame.additional_data
388+
error_string = ("Encountered error %s %s: %s" %
389+
(name, number, description))
390+
391+
raise ConnectionError(error_string)
392392

393-
raise ConnectionError(
394-
"Encountered error %s %s: %s." % (err_name, err_no,
395-
err_description)
396-
)
397393
elif frame.type == BlockedFrame.type:
398394
increment = self.window_manager._blocked()
399395
if increment:
Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22
"""
3-
hyper/http20/error_code_registry
4-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
hyper/http20/errorserrors
4+
~~~~~~~~~~~~~~~~~~~
55
66
Global error code registry containing the established HTTP/2 error codes.
77
The registry is based on a 32-bit space so we use the error code to index into
@@ -12,38 +12,64 @@
1212
"""
1313

1414
NO_ERROR = {'Name': 'NO_ERROR',
15+
'Code': '0x0',
1516
'Description': 'Graceful shutdown'}
1617
PROTOCOL_ERROR = {'Name': 'PROTOCOL_ERROR',
18+
'Code': '0x1',
1719
'Description': 'Protocol error detected'}
1820
INTERNAL_ERROR = {'Name': 'INTERNAL_ERROR',
21+
'Code': '0x2',
1922
'Description': 'Implementation fault'}
2023
FLOW_CONTROL_ERROR = {'Name': 'FLOW_CONTROL_ERROR',
24+
'Code': '0x3',
2125
'Description': 'Flow control limits exceeded'}
2226
SETTINGS_TIMEOUT = {'Name': 'SETTINGS_TIMEOUT',
27+
'Code': '0x4',
2328
'Description': 'Settings not acknowledged'}
2429
STREAM_CLOSED = {'Name': 'STREAM_CLOSED',
30+
'Code': '0x5',
2531
'Description': 'Frame received for closed stream'}
2632
FRAME_SIZE_ERROR = {'Name': 'FRAME_SIZE_ERROR',
33+
'Code': '0x6',
2734
'Description': 'Frame size incorrect'}
2835
REFUSED_STREAM = {'Name': 'REFUSED_STREAM ',
36+
'Code': '0x7',
2937
'Description': 'Stream not processed'}
3038
CANCEL = {'Name': 'CANCEL',
39+
'Code': '0x8',
3140
'Description': 'Stream cancelled'}
3241
COMPRESSION_ERROR = {'Name': 'COMPRESSION_ERROR',
42+
'Code': '0x9',
3343
'Description': 'Compression state not updated'}
3444
CONNECT_ERROR = {'Name': 'CONNECT_ERROR',
45+
'Code': '0xa',
3546
'Description':
3647
'TCP connection error for CONNECT method'}
3748
ENHANCE_YOUR_CALM = {'Name': 'ENHANCE_YOUR_CALM',
49+
'Code': '0xb',
3850
'Description': 'Processing capacity exceeded'}
3951
INADEQUATE_SECURITY = {'Name': 'INADEQUATE_SECURITY',
52+
'Code': '0xc',
4053
'Description':
4154
'Negotiated TLS parameters not acceptable'}
4255
HTTP_1_1_REQUIRED = {'Name': 'HTTP_1_1_REQUIRED',
56+
'Code': '0xd',
4357
'Description': 'Use HTTP/1.1 for the request'}
4458

45-
H2_ERROR_CODE_REGISTRY = [NO_ERROR, PROTOCOL_ERROR, INTERNAL_ERROR,
46-
FLOW_CONTROL_ERROR, SETTINGS_TIMEOUT, STREAM_CLOSED,
47-
FRAME_SIZE_ERROR, REFUSED_STREAM, CANCEL,
48-
COMPRESSION_ERROR, CONNECT_ERROR, ENHANCE_YOUR_CALM,
49-
INADEQUATE_SECURITY, HTTP_1_1_REQUIRED]
59+
H2_ERRORS = [NO_ERROR, PROTOCOL_ERROR, INTERNAL_ERROR, FLOW_CONTROL_ERROR,
60+
SETTINGS_TIMEOUT, STREAM_CLOSED, FRAME_SIZE_ERROR, REFUSED_STREAM,
61+
CANCEL, COMPRESSION_ERROR, CONNECT_ERROR, ENHANCE_YOUR_CALM,
62+
INADEQUATE_SECURITY, HTTP_1_1_REQUIRED]
63+
64+
def get_data(error_code):
65+
"""
66+
Lookup the error code description, if not available throw a value error
67+
"""
68+
if error_code < 0 or error_code >= len(H2_ERRORS):
69+
raise ValueError("Error code is invalid")
70+
71+
name = H2_ERRORS[error_code]['Name']
72+
number = H2_ERRORS[error_code]['Code']
73+
description = H2_ERRORS[error_code]['Description']
74+
75+
return name, number, description

0 commit comments

Comments
 (0)