Skip to content

update_resource returns 500 error when absolute url is external #285

@Reinaard

Description

@Reinaard

Describe the bug, issue or concern

A clear and concise description of what the bug, issue or concern is is.

E.g.

To Reproduce

  1. Go to FHIR store and update Organization by changing the reference Endpoint/default-2 with PUT like this:
{
  "resourceType": "Organization",
  "id": "default-3",
  "endpoint": [
    {
      "reference": "http://some.server:9504/fhir/Endpoint/default-2"
    }
  ]
}
  1. Go to mcsd-update-client and update resource
  2. The following 500 internal server error appears:
INFO:     185.93.2.245:60473 - "POST /update_resources HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/fastapi/applications.py", line 1134, in __call__
    await super().__call__(scope, receive, send)
  File "/usr/local/lib/python3.11/site-packages/starlette/applications.py", line 113, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 186, in __call__
    raise exc
  File "/usr/local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 164, in __call__
    await self.app(scope, receive, _send)
  File "/usr/local/lib/python3.11/site-packages/opentelemetry/instrumentation/asgi/__init__.py", line 743, in __call__
    await self.app(scope, otel_receive, otel_send)
  File "/usr/local/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 63, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "/usr/local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    raise exc
  File "/usr/local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
    await app(scope, receive, sender)
  File "/usr/local/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 716, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 736, in app
    await route.handle(scope, receive, send)
  File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 290, in handle
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.11/site-packages/fastapi/routing.py", line 125, in app
    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  File "/usr/local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    raise exc
  File "/usr/local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
    await app(scope, receive, sender)
  File "/usr/local/lib/python3.11/site-packages/fastapi/routing.py", line 111, in app
    response = await f(request)
               ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/fastapi/routing.py", line 391, in app
    raw_response = await run_endpoint_function(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/fastapi/routing.py", line 292, in run_endpoint_function
    return await run_in_threadpool(dependant.call, **values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/starlette/concurrency.py", line 38, in run_in_threadpool
    return await anyio.to_thread.run_sync(func)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/anyio/to_thread.py", line 56, in run_sync
    return await get_async_backend().run_sync_in_worker_thread(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 2485, in run_sync_in_worker_thread
    return await future
           ^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 976, in run
    result = context.run(func, *args)
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/src/app/routers/update_router.py", line 35, in update_all_directories
    return [service.update(directory, since, ura_whitelist) for directory in directories]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/src/app/routers/update_router.py", line 35, in <listcomp>
    return [service.update(directory, since, ura_whitelist) for directory in directories]
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/src/app/services/update/update_client_service.py", line 166, in update
    self.update_resource(directory, res.value, cache_service, since, ura_whitelist)
  File "/src/app/services/update/update_client_service.py", line 228, in update_resource
    nodes = self.update_page(targets, adjacency_map_service)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/src/app/services/update/update_client_service.py", line 243, in update_page
    adj_map = adjacency_map_service.build_adjacency_map(entries)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/src/app/services/update/adjacency_map_service.py", line 67, in build_adjacency_map
    nodes = [self.create_node(entry) for entry in entries]
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/src/app/services/update/adjacency_map_service.py", line 67, in <listcomp>
    nodes = [self.create_node(entry) for entry in entries]
             ^^^^^^^^^^^^^^^^^^^^^^^
  File "/src/app/services/update/adjacency_map_service.py", line 208, in create_node
    references = self.create_node_references(entry)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/src/app/services/update/adjacency_map_service.py", line 230, in create_node_references
    reference_node = FhirService.make_reference_node(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/src/app/services/fhir/fhir_service.py", line 70, in make_reference_node
    return build_node_reference(data, base_url)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/src/app/services/fhir/references/reference_misc.py", line 21, in build_node_reference
    raise ValueError("Invalid absolute URL found")
ValueError: Invalid absolute URL found
  1. Return the reference to an relative url like this:
{
  "resourceType": "Organization",
  "id": "default-3",
  "endpoint": [
    {
      "reference": "Endpoint/default-2"
    }
  ]
}
  1. When doing update_resource again, we see the same error again

Expected behavior:

  • Nicer error handing (application crashes now)
  • Error to disappear when restoring the reference url to relative.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions