rest-nvim / rest.nvim

A fast Neovim http client written in Lua
GNU General Public License v3.0
1.52k stars 141 forks source link

Formatting empty body with jq results in unhandled error #478

Open jrebs opened 5 hours ago

jrebs commented 5 hours ago

Prerequisites

Neovim Version

v0.10.1

Operating system/version

Xubuntu 24.04

Actual behavior

When a request results in a 401 with an empty body, rest.vim dumps a big error trace to the user and gets stuck in a "Loading..." status that never shows the user what the response code of the request was, making it difficult to diagnose.

image

image

Expected behavior

The expectation would be that if there is no body to be formatted, rest.nvim would skip the formatting step and this would give the user a results pane that would look like:

HTTP/2 401

# @_RES

# @_END

Steps to reproduce

Run this request with

# GET Test that will return an 401 response with no body content                                          
GET https://api.attentivemobile.com/v1/me

Other information

When using the "Repro" script below, rest.nvim does not yield the error above. Instead it yields the expected result due to the fact that there is no formatting occurring on the response. If I use the repro code and request a working endpoint with valid token, I get back an expected response, but the JSON is not formatted and is just a big long string that is difficult to read.

To get formatting to work, I followed the guidance from boltlessengineer here: https://github.com/rest-nvim/rest.nvim/issues/414#issuecomment-2308910953.

That recommendation causes the rest responses to be formatted and highlighted in pretty JSON, but seems to have the side effect of rest.nvim not handling the error nicely when there is no body to format.

Repro (lazy.nvim)

vim.env.LAZY_STDPATH = ".repro"
load(vim.fn.system("curl -s https://raw.githubusercontent.com/folke/lazy.nvim/main/bootstrap.lua"))()

require("lazy.minit").repro({
    spec = {
        "rest-nvim/rest.nvim",
        {
            "nvim-treesitter/nvim-treesitter",
            build = ":TSUpdate",
            main = "nvim-treesitter.configs",
            opts = {
                ensure_installed = { "http" },
                sync_install = false,
                highlight = { enable = true },
                indent = { enable = true },
            },
        },
    },
})
boltlessengineer commented 4 hours ago

Can reproduce it. Thank you for reporting with that precise reproduce steps :)