tarantool / tarantool/http

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

Open
#195 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Lua
Stars
81
Forks
41
PR merge metrics
No merged PRs in 30d

Description

  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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the request_json entry point and inspect how req:read_cached() supplies data before json.decode is called. Compare empty-data and malformed-JSON behavior, then verify that the intended empty response and 500-error handling are covered without breaking existing request handling.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.