From 0592b36caf8af1e0a91d15da67cfadb3521761c9 Mon Sep 17 00:00:00 2001 From: Jeremy Fee <jmfee@usgs.gov> Date: Fri, 6 Nov 2020 13:10:12 -0700 Subject: [PATCH] Add usage url to errors, clean up url output --- code.json | 2 +- geomagio/api/ws/app.py | 24 ++++++++++++++++-------- setup.py | 2 +- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/code.json b/code.json index 1b1730925..1ba18f56b 100644 --- a/code.json +++ b/code.json @@ -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": { diff --git a/geomagio/api/ws/app.py b/geomagio/api/ws/app.py index e599c6642..454c918ee 100644 --- a/geomagio/api/ws/app.py +++ b/geomagio/api/ws/app.py @@ -1,9 +1,7 @@ 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} diff --git a/setup.py b/setup.py index 7f36d5525..2861d39c8 100644 --- a/setup.py +++ b/setup.py @@ -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*"]), -- GitLab