Esempio n. 1
0
    def test_from_exception_for_not_found_exception(self) -> None:
        """Test from_exception for NotFoundException."""
        message = "There's nothing here!"

        response = ResponseGenerator.from_exception(NotFoundException(message))

        self.assertEqual(404, response.status_code)
        self.assertEqual(message, response.data.decode("utf-8"))
Esempio n. 2
0
    def test_from_exception_for_exception(self) -> None:
        """Test from_exception for Exception."""
        message = "Something crashed!"

        response = ResponseGenerator.from_exception(Exception(message))

        self.assertEqual(500, response.status_code)
        self.assertEqual(message, response.data.decode("utf-8"))
Esempio n. 3
0
    def test_from_exception_for_internal_exception(self) -> None:
        """Test from_exception for InternalException."""
        message = "Domain code crashed!"

        response = ResponseGenerator.from_exception(InternalException(message))

        self.assertEqual(500, response.status_code)
        self.assertEqual(message, response.data.decode("utf-8"))
Esempio n. 4
0
    def test_from_exception_for_access_denied_exception(self) -> None:
        """Test from_exception for AccessDeniedException."""
        message = "You can't enter here!"

        response = ResponseGenerator.from_exception(
            AccessDeniedException(message))

        self.assertEqual(403, response.status_code)
        self.assertEqual(message, response.data.decode("utf-8"))
Esempio n. 5
0
    def test_from_exception_for_client_error_exception(self) -> None:
        """Test from_exception for ClientErrorException."""
        message = "Request is invalid!"

        response = ResponseGenerator.from_exception(
            ClientErrorException(message))

        self.assertEqual(400, response.status_code)
        self.assertEqual(message, response.data.decode("utf-8"))
Esempio n. 6
0
def handle_api_call(subpath: str) -> Any:
    """Handle API access."""
    try:
        parameters = build_parameters(subpath, request)
        response = router.handle(parameters)
        if isinstance(response, WebResponse):
            return response
        return jsonify(response.data)
    except Exception as err:
        if isinstance(err, InternalException):
            log.critical(err)
        return ResponseGenerator.from_exception(err)
Esempio n. 7
0
    def get_output(data: dict) -> Response:
        """Get config file for requested Workload."""
        workload_id = RequestDataProcessor.get_string_value(data, "workload_id")
        workdir = Workdir()
        workload_data = workdir.get_workload_data(workload_id)
        log_path = workload_data.get("log_path")

        if not log_path:
            raise NotFoundException(f"Unable to find output log for {workload_id}")

        try:
            response = ResponseGenerator.serve_from_filesystem(
                path=log_path,
                mimetype="text/plain",
            )
        except Exception as e:
            response = ResponseGenerator.from_exception(e)

        return ResponseGenerator.add_refresh(
            response=response,
            refresh_time=3,
        )