Skip to content

Commit dc10994

Browse files
udayvir-singhNTBBloodbath
authored andcommitted
fix: error handling for formatters
1 parent 8b7d9df commit dc10994

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,13 @@ use {
7474
show_url = true,
7575
show_http_info = true,
7676
show_headers = true,
77-
-- executables for formatting response body [optional]
77+
-- executables or functions for formatting response body [optional]
78+
-- set them to nil if you want to disable them
7879
formatters = {
79-
-- set them to nil if you want to disable them
8080
json = "jq",
81-
html = {"tidy", "-i", "-q", "--show-errors", "0", "-"}
81+
html = function(body)
82+
return vim.fn.system({"tidy", "-i", "-q", "-"}, body)
83+
end
8284
},
8385
},
8486
-- Jump to request line on run

lua/rest-nvim/config/init.lua

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ local config = {
1414
show_headers = true,
1515
formatters = {
1616
json = "jq",
17-
html = {
18-
"tidy", "-i", "-q",
19-
"--tidy-mark", "no",
20-
"--show-body-only", "auto",
21-
"--show-errors", "0",
22-
"--show-warnings", "0",
23-
"-"
24-
}
17+
html = function (body)
18+
return vim.fn.system({
19+
"tidy", "-i", "-q",
20+
"--tidy-mark", "no",
21+
"--show-body-only", "auto",
22+
"--show-errors", "0",
23+
"--show-warnings", "0",
24+
"-"
25+
}, body):gsub("\n$", "")
26+
end
2527
},
2628
},
2729
jump_to_request = false,

lua/rest-nvim/curl/init.lua

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ local curl = require("plenary.curl")
33
local config = require("rest-nvim.config")
44

55
local M = {}
6+
-- checks if 'x' can be executed by system()
7+
local function is_executable(x)
8+
if type(x) == "string" and vim.fn.executable(x) == 1 then
9+
return true
10+
elseif vim.tbl_islist(x) and vim.fn.executable(x[1] or "") == 1 then
11+
return true
12+
end
13+
14+
return false
15+
end
16+
617
-- get_or_create_buf checks if there is already a buffer with the rest run results
718
-- and if the buffer does not exists, then create a new one
819
M.get_or_create_buf = function()
@@ -89,8 +100,26 @@ local function create_callback(method, url)
89100
--- Add the curl command results into the created buffer
90101
local formatter = config.get("result").formatters[content_type]
91102
-- formate response body
92-
if formatter and vim.fn.executable(type(formatter) == "string" and formatter or formatter[1]) == 1 then
93-
res.body = vim.fn.system(formatter, res.body):gsub("\n$", "")
103+
if type(formatter) == "function" then
104+
local ok, out = pcall(formatter, res.body)
105+
-- check if formatter ran successfully
106+
if ok and out then
107+
res.body = out
108+
else
109+
vim.api.nvim_echo({{
110+
string.format("Error calling formatter on response body:\n%s", out), "Error"
111+
}}, false, {})
112+
end
113+
elseif is_executable(formatter) then
114+
local stdout = vim.fn.system(formatter, res.body):gsub("\n$", "")
115+
-- check if formatter ran successfully
116+
if vim.v.shell_error == 0 then
117+
res.body = stdout
118+
else
119+
vim.api.nvim_echo({{
120+
string.format("Error running formatter %s on response body:\n%s", vim.inspect(formatter), stdout), "Error"
121+
}}, false, {})
122+
end
94123
end
95124

96125
-- append response container

0 commit comments

Comments
 (0)