def method_not_allowed(self, error): """ This method handles an exception error when the HTTP method sent is not supported. Parameters ---------- error: Exception object (required) the exception error object. Returns ------- an HTTP response object with appropriate error message. """ logging.debug("inside method_not_allowed") code = "" if hasattr(error, 'code'): code = getattr(error, 'code') logging.debug("Handling a status_code [{0}] error [{1}]" .format(code, error)) method = request.method # if a description was found on the error, use that one instead description = get_error_description(error) if is_blank(description): description = "The HTTP method [{0}] is not supported.".format(method) response = ErrorResponse.get_response(405, description, "application/json") return response
def handle_internal_server_error(self, error): """ This method handles an internal server exception error. Parameters ---------- error: Exception object (required) the exception error object. Returns ------- an HTTP response object with appropriate error message. """ logging.debug("inside handle_internal_server_error") code = 500 if hasattr(error, 'code'): code = getattr(error, 'code') logging.debug("Handling a status_code [{0}] error [{1}]" .format(code, error)) # if a description was found on the error, use that one instead description = get_error_description(error) if is_blank(description): description = "An internal server error occurred" if isinstance(error, Exception): logging.exception(error) response = ErrorResponse.get_response(code, description, "application/json") return response
def handle_not_expected(self, error): """ This method handles an exception error for an HTTP status code that is currently not supported, and it was not expected. Parameters ---------- error: Exception object (required) the exception error object. Returns ------- an HTTP response object with appropriate error message. """ logging.debug("inside handle_not_expected") code = "" if hasattr(error, 'code'): code = getattr(error, 'code') logging.debug("Handling a status_code [{0}] error [{1}]" .format(code, error)) # if a description was found on the error, use that one instead description = get_error_description(error) if is_blank(description): description = ("URL [{0}] request generate a not expected status" .format(request.url)) response = ErrorResponse.get_response(401, description, "application/json") response.headers["WWW-Authenticate"] = 'Basic realm = "IoT Gateway"' return response
def handle_request_timeout(self, error): """ This method handles an exception error when the request times out. Parameters ---------- error: Exception object (required) the exception error object. Returns ------- an HTTP response object with appropriate error message. """ logging.debug("inside handle_request_timeout") code = "" if hasattr(error, 'code'): code = getattr(error, 'code') logging.debug("Handling a status_code [{0}] error [{1}]" .format(code, error)) # if a description was found on the error, use that one instead description = get_error_description(error) if is_blank(description): description = "URL [{0}] request timed out".format(request.url) response = ErrorResponse.get_response(408, description, "application/json") response.headers["WWW-Authenticate"] = 'Basic realm = "IoT Gateway"' return response
def bad_request(self, error): """ This method handles an exception error when the HTTP request could not be served due to a malformed request (e.g., some parameters are not valid). Parameters ---------- error: Exception object (required) the exception error object. Returns ------- an HTTP response object with appropriate error message. """ logging.debug("inside bad_request") code = "" if hasattr(error, 'code'): code = getattr(error, 'code') logging.debug("Handling a status_code [{0}] error [{1}]" .format(code, error)) # if a description was found on the error, use that one instead description = get_error_description(error) if is_blank(description): description = "Bad URL [{0}] request".format(request.url) response = ErrorResponse.get_response(400, description, "application/json") return response
def handle_not_acceptable(self, error): """ This method handles an exception error when none of the media types defined in the incoming HTTP Accept header is supported by this REST API. Parameters ---------- error: Exception object (required) the exception error object. Returns ------- an HTTP response object with appropriate error message. """ logging.debug("inside handle_not_acceptable") code = "" if hasattr(error, 'code'): code = getattr(error, 'code') logging.debug("Handling a status_code [{0}] error [{1}]" .format(code, error)) mimeType = request.accept_mimetypes.best logging.debug("Handling a 406 error [{0}]. " "The best mime type is [{1}]" .format(error, mimeType)) accept = request.headers['Accept'] # if a description was found on the error, use that one instead description = get_error_description(error) if is_blank(description): description = ("Requested MIME types [{0}] not supported." .format(accept)) response = ErrorResponse.get_response(406, description, mimeType) return response
def handle_not_found(self, error): """ This method handles an exception error when the requested URL resource was not found on the server. Parameters ---------- error: Exception object (required) the exception error object. Returns ------- an HTTP response object with appropriate error message. """ logging.debug("inside handle_not_found") code = "" if hasattr(error, 'code'): code = getattr(error, 'code') logging.debug("Handling a status_code [{0}] error [{1}]" .format(code, error)) url_not_found = request.url logging.debug("Handling a URL [{0}] not found error [{1}]" .format(url_not_found, error)) # if a description was found on the error, use that one instead description = get_error_description(error) if is_blank(description): description = ("Requested resource identified by [{0}] not found." .format(url_not_found)) response = ErrorResponse.get_response(404, description, "application/json") return response