@@ -31,6 +31,29 @@ local function format_curl_cmd(res)
3131 return cmd
3232end
3333
34+ local function send_curl_start_event (data )
35+ vim .api .nvim_exec_autocmds (" User" , {
36+ pattern = " RestStartRequest" ,
37+ modeline = false ,
38+ data = data ,
39+ })
40+ end
41+
42+ local function send_curl_stop_event (data )
43+ vim .api .nvim_exec_autocmds (" User" , {
44+ pattern = " RestStopRequest" ,
45+ modeline = false ,
46+ data = data ,
47+ })
48+ end
49+
50+ local function create_error_handler (opts )
51+ return function (err )
52+ send_curl_stop_event (vim .tbl_extend (" keep" , { err = err }, opts ))
53+ vim .notify (vim .inspect (err .message ), vim .log .levels .ERROR )
54+ end
55+ end
56+
3457-- get_or_create_buf checks if there is already a buffer with the rest run results
3558-- and if the buffer does not exists, then create a new one
3659M .get_or_create_buf = function ()
@@ -64,12 +87,17 @@ M.get_or_create_buf = function()
6487 vim .api .nvim_set_option_value (" ft" , " httpResult" , { buf = new_bufnr })
6588 vim .api .nvim_set_option_value (" buftype" , " nofile" , { buf = new_bufnr })
6689
67-
6890 return new_bufnr
6991end
7092
71- local function create_callback (curl_cmd , method , url , script_str )
93+ local function create_callback (curl_cmd , opts )
94+ local method = opts .method
95+ local url = opts .url
96+ local script_str = opts .script_str
97+
7298 return function (res )
99+ send_curl_stop_event (vim .tbl_extend (" keep" , { res = res }, opts ))
100+
73101 if res .exit ~= 0 then
74102 log .error (" [rest.nvim] " .. utils .curl_error (res .exit ))
75103 return
@@ -234,16 +262,20 @@ M.curl_cmd = function(opts)
234262 local res = curl [opts .method ](dry_run_opts )
235263 local curl_cmd = format_curl_cmd (res )
236264
265+ send_curl_start_event (opts )
266+
237267 if opts .dry_run then
238268 if config .get (" yank_dry_run" ) then
239269 vim .cmd (" let @+=" .. string.format (" %q" , curl_cmd ))
240270 end
241271
242272 vim .api .nvim_echo ({ { " [rest.nvim] Request preview:\n " , " Comment" }, { curl_cmd } }, false , {})
273+
274+ send_curl_stop_event (opts )
243275 return
244276 else
245- opts .callback =
246- vim .schedule_wrap (create_callback ( curl_cmd , opts . method , opts . url , opts . script_str ))
277+ opts .callback = vim . schedule_wrap ( create_callback ( curl_cmd , opts ))
278+ opts . on_error = vim .schedule_wrap (create_error_handler ( opts ))
247279 curl [opts .method ](opts )
248280 end
249281end
0 commit comments