Skip to content
Snippets Groups Projects
Commit 0592b36c authored by Jeremy M Fee's avatar Jeremy M Fee
Browse files

Add usage url to errors, clean up url output

parent 6cc972ae
No related branches found
Tags 1.3.4
1 merge request!146Release CMO metadata to production
Pipeline #25996 passed with warnings
......@@ -3,7 +3,7 @@
"name": "geomag-algorithms",
"organization": "U.S. Geological Survey",
"description": "Library for processing Geomagnetic timeseries data.",
"version": "1.3.3",
"version": "1.3.4",
"status": "Development",
"permissions": {
......
import os
from typing import Dict, Union
from fastapi import FastAPI, Request, Response
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse, PlainTextResponse, RedirectResponse
from obspy import UTCDateTime
......@@ -78,13 +76,22 @@ def format_error(
status_code: int, exception: str, format: str, request: Request
) -> Response:
"""Assign error_body value based on error format."""
# These urls are embedded in responses
# and app usually runs behind reverse proxy
url = str(request.url)
usage = f"http://{request.headers['host']}/ws/docs"
if "x-forwarded-proto" in request.headers:
proto = f"{request.headers['x-forwarded-proto']}:"
url = url.replace("http:", proto)
usage = usage.replace("http:", proto)
# serve error
if format == "json":
return json_error(status_code, exception, request.url)
return json_error(code=status_code, error=exception, url=url, usage=usage)
else:
return text_error(status_code, exception, request.url)
return text_error(code=status_code, error=exception, url=url, usage=usage)
def json_error(code: int, error: Exception, url: str) -> Response:
def json_error(code: int, error: Exception, url: str, usage: str) -> Response:
"""Format json error message.
Returns
......@@ -100,7 +107,8 @@ def json_error(code: int, error: Exception, url: str) -> Response:
"status": code,
"error": str(error),
"generated": f"{UTCDateTime().isoformat()}Z",
"url": str(url),
"url": url,
"usage": usage,
"version": VERSION,
},
},
......@@ -108,7 +116,7 @@ def json_error(code: int, error: Exception, url: str) -> Response:
)
def text_error(code: int, error: Exception, url: str) -> Response:
def text_error(code: int, error: Exception, url: str, usage: str = "") -> Response:
"""Format error message as plain text
Returns
......@@ -120,7 +128,7 @@ def text_error(code: int, error: Exception, url: str) -> Response:
{error}
Usage details are available from
Usage details are available from {usage}
Request:
{url}
......
......@@ -9,7 +9,7 @@ if ssl_cert_file:
setuptools.setup(
name="geomag-algorithms",
version="1.3.3",
version="1.3.4",
description="USGS Geomag Algorithms Library",
url="https://github.com/usgs/geomag-algorithms",
packages=setuptools.find_packages(exclude=["test*"]),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment