> This page is for version 0.7.0.
> For other versions, use one of these documentation indexes:
> - Main (default): https://docs.nvidia.com/nemo/gym/main/llms.txt
> - 0.7.0: https://docs.nvidia.com/nemo/gym/v0.7.0/llms.txt
> - 0.6.0: https://docs.nvidia.com/nemo/gym/v0.6.0/llms.txt
> - 0.5.1: https://docs.nvidia.com/nemo/gym/v0.5.1/llms.txt
> - 0.5.0: https://docs.nvidia.com/nemo/gym/v0.5.0/llms.txt
> - 0.4.0: https://docs.nvidia.com/nemo/gym/v0.4.0/llms.txt
> - 0.3.0: https://docs.nvidia.com/nemo/gym/v0.3.0/llms.txt
> - 0.2.1: https://docs.nvidia.com/nemo/gym/v0.2.1/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/gym/llms.txt.

# nemo_gym.server_runtime.request_handling.exception_handling

Middleware that answers a failed request and reports every request's failures in its reply.

## Module Contents

### Classes

| Name                                                                                                                      | Description                                                                                       |
| ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
| [`ExceptionHandlingMiddleware`](#nemo_gym-server_runtime-request_handling-exception_handling-ExceptionHandlingMiddleware) | Answer a request whose route failed before replying, and send each reply with its failure report. |

### Functions

| Name                                                                                                                      | Description                                                                                 |
| ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------- |
| [`_add_failure_header`](#nemo_gym-server_runtime-request_handling-exception_handling-_add_failure_header)                 | Put `report` in a reply's failure header, merged with any report the reply already carries. |
| [`_is_cancelling_this_request`](#nemo_gym-server_runtime-request_handling-exception_handling-_is_cancelling_this_request) | Whether `error` is the cancellation of the task serving this request.                       |

### API

```python
class nemo_gym.server_runtime.request_handling.exception_handling.ExceptionHandlingMiddleware(
    app: starlette.types.ASGIApp,
    failure_reply: typing.Callable[[BaseException], fastapi.responses.JSONResponse]
)
```

Answer a request whose route failed before replying, and send each reply with its failure report.

A route that raises, or returns without replying, gets `failure_reply`'s response.
That response keeps the partial observations the route left in the request state.
Every reply, successful or not, carries the failures observed while serving it in its failure header.
Once a reply has started, its status is already sent, so a later failure propagates.
A cancelled request still gets the failure reply, and then the cancellation propagates to whoever cancelled the
task.

```python
nemo_gym.server_runtime.request_handling.exception_handling.ExceptionHandlingMiddleware.__call__(
    scope: starlette.types.Scope,
    receive: starlette.types.Receive,
    send: starlette.types.Send
) -> None
```

async

```python
nemo_gym.server_runtime.request_handling.exception_handling.ExceptionHandlingMiddleware._failure_response(
    scope: starlette.types.Scope,
    error: BaseException
) -> fastapi.responses.JSONResponse
```

```python
nemo_gym.server_runtime.request_handling.exception_handling._add_failure_header(
    message: starlette.types.Message,
    report: nemo_gym.core.failure_records.FailureReport
) -> None
```

Put `report` in a reply's failure header, merged with any report the reply already carries.

```python
nemo_gym.server_runtime.request_handling.exception_handling._is_cancelling_this_request(
    error: BaseException
) -> bool
```

Whether `error` is the cancellation of the task serving this request.

Gym cancels that task when a client disconnects, when a checkpoint retires an activation or a model call, and when
uvicorn shuts down.