atviriduomenys / spinta

Spinta is a framework to describe, extract and publish data (a DEP Framework).
MIT License
10 stars 4 forks source link

Improve missing resource name error message #525

Open sirex opened 8 months ago

sirex commented 8 months ago

Currently if model.source name is not specified, following error message is show:

Traceback (most recent call last):
  File "spinta/api.py", line 95, in homepage
    return await create_http_response(context, params, request)
  File "spinta/utils/response.py", line 166, in create_http_response
    return await commands.getall(
  File "spinta/commands/read.py", line 141, in getall
    return render(context, request, model, params, rows, action=action)
  File "spinta/renderer.py", line 23, in render
    return commands.render(
  File "spinta/formats/html/commands.py", line 143, in render
    return _render_model(context, request, model, action, params, data, headers)
  File "spinta/formats/html/commands.py", line 247, in _render_model
    ctx = build_template_context(
  File "spinta/formats/html/commands.py", line 221, in build_template_context
    rows, empty = _is_empty(rows)
  File "spinta/formats/html/commands.py", line 175, in _is_empty
    row = next(rows)
  File "spinta/utils/nestedstruct.py", line 15, in flatten
    for v in vals:
  File "spinta/accesslog/__init__.py", line 162, in log_response
    for row in rows:
  File "spinta/commands/read.py", line 126, in <genexpr>
    rows = (
  File "spinta/datasets/backends/sql/commands/read.py", line 90, in getall
    table = model.external.name.format(**params)
AttributeError: 'NoneType' object has no attribute 'format'

This error show be detected at load or check time and a more informative error message should be shown.

This error message specifically appears, when reading manifest from XLSX format, since in XLSX empty cells are read as None, and from CSV they are read as ''. So in XLSX, if model.source is not given, then model.external becomes None and ends up with this error.

The correct error should be:

HTTP/1.1 404 Not Found

{
    "errors": [
        {
            "code": "SourceTableNotFound",
            "context": {
                "model": "example/Table"
                "source": ""
            },
            "message": "Source table '' for model 'example/Table' not found.",
            "template": "Source table {source!r} for model {model!r} not found.",
            "type": "model"
        }
    ]
}