Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
uriid1 opened this issue Dec 7, 2023 · 0 comments
Open

Comments

@uriid1
Copy link

uriid1 commented Dec 7, 2023

  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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant