http icon indicating copy to clipboard operation
http copied to clipboard

Suggestion for Handling Empty Data and JSON Decoding in `request_json`

Open uriid1 opened this issue 1 year ago • 0 comments

  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.

uriid1 avatar Dec 07 '23 14:12 uriid1