ding.interaction.exception.slave¶
ding.interaction.exception.slave
¶
SlaveErrorCode
¶
Bases: IntEnum
Overview
Error code for slave end
SlaveResponseException
¶
Bases: ResponseException
Overview
Response exception for slave client
__init__(error)
¶
Overview
Constructor
Arguments:
- error (:obj:HTTPError): Original http exception object
get_slave_exception_class_by_error_code(error_code)
¶
Overview
Transform from slave error code to SlaveResponseException class
Arguments:
- error_code (:obj:SlaveErrorCode): Slave error code
Returns:
- exception_class (:obj:Type[SlaveResponseException): Slave response exception class
get_slave_exception_by_error(error)
¶
Overview
Auto transform http error object to slave response exception object.
Arguments:
- error (:obj:HTTPError): Http error object
Returns:
- exception (:obj:SlaveResponseException): Slave response exception object
Full Source Code
../ding/interaction/exception/slave.py
1from abc import ABCMeta 2from enum import unique, IntEnum 3from typing import Type 4 5import enum_tools 6from requests import HTTPError 7 8from .base import ResponseException 9from ..base import get_values_from_response 10 11 12@enum_tools.documentation.document_enum 13@unique 14class SlaveErrorCode(IntEnum): 15 """ 16 Overview: 17 Error code for slave end 18 """ 19 SUCCESS = 0 # doc: Slave request success 20 21 SYSTEM_SHUTTING_DOWN = 101 # doc: Slave end is shutting down 22 23 CHANNEL_NOT_FOUND = 201 # doc: No channel id given in request 24 CHANNEL_INVALID = 202 # doc: Channel id given not match with slave end 25 26 MASTER_TOKEN_NOT_FOUND = 301 # doc: No master token found in connection request from master 27 MASTER_TOKEN_INVALID = 302 # doc: Master token auth failed in slave end 28 29 SELF_TOKEN_NOT_FOUND = 401 # doc: No self token given in self request (such as ping, shutdown) 30 SELF_TOKEN_INVALID = 402 # doc: Self token auth failed in slave end itself (such as ping, shutdown) 31 32 SLAVE_ALREADY_CONNECTED = 501 # doc: Slave end has already connected to another master end 33 SLAVE_NOT_CONNECTED = 502 # doc: Slave end not connected with master end yey 34 SLAVE_CONNECTION_REFUSED = 503 # doc: Connection to slave end refused 35 SLAVE_DISCONNECTION_REFUSED = 504 # doc: Disconnection to slave end refused 36 37 TASK_ALREADY_EXIST = 601 # doc: Slave end is processing another task 38 TASK_REFUSED = 602 # doc: Task for slave end refused 39 40 41# noinspection DuplicatedCode 42class SlaveResponseException(ResponseException, metaclass=ABCMeta): 43 """ 44 Overview: 45 Response exception for slave client 46 """ 47 48 def __init__(self, error: HTTPError): 49 """ 50 Overview: 51 Constructor 52 Arguments: 53 - error (:obj:`HTTPError`): Original http exception object 54 """ 55 ResponseException.__init__(self, error) 56 57 58class SlaveSuccess(SlaveResponseException): 59 pass 60 61 62class SlaveSystemShuttingDown(SlaveResponseException): 63 pass 64 65 66class SlaveChannelNotFound(SlaveResponseException): 67 pass 68 69 70class SlaveChannelInvalid(SlaveResponseException): 71 pass 72 73 74class SlaveMasterTokenNotFound(SlaveResponseException): 75 pass 76 77 78class SlaveMasterTokenInvalid(SlaveResponseException): 79 pass 80 81 82class SlaveSelfTokenNotFound(SlaveResponseException): 83 pass 84 85 86class SlaveSelfTokenInvalid(SlaveResponseException): 87 pass 88 89 90class SlaveSlaveAlreadyConnected(SlaveResponseException): 91 pass 92 93 94class SlaveSlaveNotConnected(SlaveResponseException): 95 pass 96 97 98class SlaveSlaveConnectionRefused(SlaveResponseException): 99 pass 100 101 102class SlaveSlaveDisconnectionRefused(SlaveResponseException): 103 pass 104 105 106class SlaveTaskAlreadyExist(SlaveResponseException): 107 pass 108 109 110class SlaveTaskRefused(SlaveResponseException): 111 pass 112 113 114_PREFIX = ['slave'] 115 116 117def get_slave_exception_class_by_error_code(error_code: SlaveErrorCode) -> Type[SlaveResponseException]: 118 """ 119 Overview: 120 Transform from slave error code to `SlaveResponseException` class 121 Arguments: 122 - error_code (:obj:`SlaveErrorCode`): Slave error code 123 Returns: 124 - exception_class (:obj:`Type[SlaveResponseException`): Slave response exception class 125 """ 126 class_name = ''.join([word.lower().capitalize() for word in (_PREFIX + error_code.name.split('_'))]) 127 return eval(class_name) 128 129 130def get_slave_exception_by_error(error: HTTPError) -> SlaveResponseException: 131 """ 132 Overview: 133 Auto transform http error object to slave response exception object. 134 Arguments: 135 - error (:obj:`HTTPError`): Http error object 136 Returns: 137 - exception (:obj:`SlaveResponseException`): Slave response exception object 138 """ 139 _, _, code, _, _ = get_values_from_response(error.response) 140 error_code = {v.value: v for k, v in SlaveErrorCode.__members__.items()}[code] 141 return get_slave_exception_class_by_error_code(error_code)(error)