Skip to content

Commit

Permalink
chore: do not log stack traces for HttpException (#235)
Browse files Browse the repository at this point in the history
This improves on
#224. My first
attempt dumps the stack for any HttpException, which means any explicit
400, etc, that we raise. What we really want is the stack trace for
unexpected errors, that is, anything that isn't HttpException.

* Move the stack dump to the base Exception handler
* Log `e.detail` in the HttpException handler. This is also important -
we want to see what the user ran into.
* Disable the `uvicorn.error` logger, which also dumps stack on any
exception. We want that to go through our handler.

To test, use the curl commands from the last pr.

The HttpException (filetype not supported error) should cleanly show as
a one liner:
```
2023-09-15 12:06:13,570 unstructured_api ERROR Unable to process logger_config.yaml: File type None is not supported.
2023-09-15 12:06:13,570 127.0.0.1:58003 POST /general/v0/general HTTP/1.1 - 400 Bad Request
```

The 500 error should show this one liner along with *a single* instance
of the stack:
```
  File "/Users/austin/repos/pipeline-api/prepline_general/api/general.py", line 386, in pipeline_api
    raise e
  File "/Users/austin/repos/pipeline-api/prepline_general/api/general.py", line 367, in pipeline_api
    elements = partition(
  File "/Users/austin/.pyenv/versions/pipeline-api/lib/python3.10/site-packages/unstructured/partition/auto.py", line 348, in partition
    raise ValueError(
ValueError: Detected a JSON file that does not conform to the Unstructured schema. partition_json currently only processes serialized Unstructured output.

2023-09-15 12:00:09,585 unstructured_api ERROR Detected a JSON file that does not conform to the Unstructured schema. partition_json currently only processes serialized Unstructured output.
2023-09-15 12:00:09,585 127.0.0.1:57735 POST /general/v0/general HTTP/1.1 - 500 Internal Server Error
```
  • Loading branch information
awalker4 authored Sep 15, 2023
1 parent ee82bb7 commit 2ed0f68
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
## 0.0.44-dev1
## 0.0.44

* Bump unstructured to 0.10.14
* Improve parallel mode retry handling
* Improve logging during error handling. We don't need to log stack traces for expected errors.

## 0.0.43

Expand Down
25 changes: 16 additions & 9 deletions prepline_general/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,16 @@
openapi_url="/general/openapi.json",
)

# Note(austin) - This logger just dumps exceptions
# We'd rather handle those below
uvicorn_logger = logging.getLogger("uvicorn.error")
uvicorn_logger.disabled = True


# Catch all HTTPException for uniform logging and response
@app.exception_handler(HTTPException)
async def http_error_handler(request: Request, e: HTTPException):
trace = traceback.format_exc()

# Note(austin) - If ENV is set, dump the stack in json
# for nicer parsing. Soon we'll just have a json logger do this.
if os.environ.get("ENV") in ["dev", "prod"]:
trace = json.dumps(trace)

logger.error(trace)
logger.error(e.detail)

return JSONResponse(
status_code=e.status_code,
Expand All @@ -38,9 +36,18 @@ async def http_error_handler(request: Request, e: HTTPException):


# Note(austin) - Convert any other errors to HTTPException
# to be handled above
# to be handled above, and log the stack trace
@app.exception_handler(Exception)
async def error_handler(request: Request, e: Exception):
trace = traceback.format_exc()

# Note(austin) - If ENV is set, dump the stack in json
# for nicer parsing. Soon we'll just have a json logger do this.
if os.environ.get("ENV") in ["dev", "prod"]:
trace = json.dumps(trace)

logger.error(trace)

error = HTTPException(
status_code=500,
detail=str(e)
Expand Down

0 comments on commit 2ed0f68

Please sign in to comment.