Skip to content

Suggestion for Handling Empty Data and JSON Decoding in request_json #195

Open
@uriid1

Description

@uriid1
  1. Checking for empty data before attempting decoding, which can prevent unnecessary errors.
  2. More explicit handling of the case when the data is empty.

For example:

local function request_json(req)
    local data = req:read_cached()
    if data == '' then
        return {}
    end

    local s, json = pcall(json.decode, data)
    if not s then
        error(sprintf("Can't decode json in request '%s': %s",
           data, tostring(json)))
       return nil
    end
    return json
end

This logic introduces optimization by avoiding unnecessary pcall invocation. It also allows handling a 500 error. However, of course, this requires attention from the developers of tarantool/http. I do not guarantee that the above code will not break some logic.

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