Skip to content

How can I have the API return a 500 status code on error? #57

@andb0t

Description

@andb0t

Hi everybody!

I am following the official tutorials and thus my run function looks like this:

@input_schema('data', StandardPythonParameterType(input_sample))
@output_schema(StandardPythonParameterType(output_sample))
def run(data):
    try:
        result = model.predict(data)
        return [result]
    except Exception as e:
        error = str(e)
        return error

So everything runs fine if the there is no error. But in case of error (e.g. the user inputing the data in the wrong format) the API returns a 200 status code as well accompanied by the string error message as output. This behavior is clear from reading the code. My question is now: how can I make the API return a 500 on error and output the error message?

I tried something like this, but then the 500 was simply included in the output string and it still returned a 200:

        return error, 500

This happily gives me the string ["some error message", 500] with a 200 status code :)

Any idea how I can achieve this?
Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions