@@ -76,8 +76,12 @@ local function get_body(bufnr, stop_line, query_line, json_body)
7676 if start_line > 0 then
7777 local json_string = ' '
7878 local json_lines = {}
79- json_lines =
80- api .nvim_buf_get_lines (bufnr , start_line , end_line - 1 , false )
79+ json_lines = api .nvim_buf_get_lines (
80+ bufnr ,
81+ start_line ,
82+ end_line - 1 ,
83+ false
84+ )
8185
8286 for _ , v in ipairs (json_lines ) do
8387 json_string = json_string .. utils .replace_env_vars (v )
@@ -124,24 +128,28 @@ local function get_headers(bufnr, query_line)
124128 break_loops = true
125129 break
126130 else
127- local get_header =
128- api .nvim_buf_get_lines (bufnr , start_line - 1 , end_line , false )
131+ local get_header = api .nvim_buf_get_lines (
132+ bufnr ,
133+ start_line - 1 ,
134+ end_line ,
135+ false
136+ )
129137
130138 for _ , header in ipairs (get_header ) do
131139 header = utils .split (header , ' :' )
132140 if
133141 header [1 ]:lower () ~= ' accept'
134142 and header [1 ]:lower () ~= ' authorization'
135- -- If header key doesn't contains double quotes,
136- -- so we don't get body keys
143+ -- If header key doesn't contains double quotes,
144+ -- so we don't get body keys
137145 and header [1 ]:find (' "' ) == nil
138- -- If header key doesn't contains hashes,
139- -- so we don't get commented headers
140- and header [1 ]:find (' ^#' ) == nil
141- -- If header key doesn't contains HTTP methods,
142- -- so we don't get the http method/url
143- and not utils .has_value (http_methods , header [1 ])
144- then
146+ -- If header key doesn't contains hashes,
147+ -- so we don't get commented headers
148+ and header [1 ]:find (' ^#' ) == nil
149+ -- If header key doesn't contains HTTP methods,
150+ -- so we don't get the http method/url
151+ and not utils .has_value (http_methods , header [1 ])
152+ then
145153 headers [header [1 ]:lower ()] = header [2 ]
146154 end
147155 end
@@ -166,8 +174,12 @@ local function get_accept(bufnr, query_line)
166174 -- Case-insensitive search
167175 local start_line = fn .search (' \\ cAccept:' , ' ' , stop_line )
168176 local end_line = start_line
169- local accept_line =
170- api .nvim_buf_get_lines (bufnr , start_line - 1 , end_line , false )
177+ local accept_line = api .nvim_buf_get_lines (
178+ bufnr ,
179+ start_line - 1 ,
180+ end_line ,
181+ false
182+ )
171183
172184 for _ , accept_data in pairs (accept_line ) do
173185 accept = utils .split (accept_data , ' :' )[2 ]
@@ -176,15 +188,15 @@ local function get_accept(bufnr, query_line)
176188
177189 go_to_line (bufnr , query_line )
178190
179- return accept
191+ return accept
180192end
181193
182194-- get_auth retrieves the HTTP Authorization and returns a lua table with its values
183195-- @param bufnr Buffer number, a.k.a id
184196-- @param query_line Line to set cursor position
185197local function get_auth (bufnr , query_line )
186198 local auth = {}
187- local auth_not_empty = false
199+ local auth_not_empty = false
188200 -- Set stop at end of bufer
189201 local stop_line = fn .line (' $' )
190202
@@ -193,8 +205,12 @@ local function get_auth(bufnr, query_line)
193205 -- Case-insensitive search
194206 local start_line = fn .search (' \\ cAuthorization:' , ' ' , stop_line )
195207 local end_line = start_line
196- local auth_line =
197- api .nvim_buf_get_lines (bufnr , start_line - 1 , end_line , false )
208+ local auth_line = api .nvim_buf_get_lines (
209+ bufnr ,
210+ start_line - 1 ,
211+ end_line ,
212+ false
213+ )
198214
199215 for _ , auth_data in pairs (auth_line ) do
200216 -- Split by spaces, e.g. {'Authorization:', 'user:pass'}
@@ -205,10 +221,10 @@ local function get_auth(bufnr, query_line)
205221 end
206222
207223 go_to_line (bufnr , query_line )
208- if not auth_not_empty then
209- return nil
210- end
211- return auth
224+ if not auth_not_empty then
225+ return nil
226+ end
227+ return auth
212228end
213229
214230-- curl_cmd runs curl with the passed options, gets or creates a new buffer
@@ -259,7 +275,7 @@ local function curl_cmd(opts)
259275 --- Add the curl command results into the created buffer
260276 if json_body then
261277 -- format JSON body
262- res .body = fn .system (" jq " , res .body )
278+ res .body = fn .system (' jq ' , res .body )
263279 end
264280 local lines = utils .split (res .body , ' \n ' )
265281 line_count = api .nvim_buf_line_count (res_bufnr ) - 1
@@ -290,12 +306,11 @@ local function run()
290306 local parsed_url = parse_url (fn .getline (' .' ))
291307 local last_query_line_number = fn .line (' .' )
292308
293- local next_query =
294- fn .search (
295- ' GET\\ |POST\\ |PUT\\ |PATCH\\ |DELETE' ,
296- ' n' ,
297- fn .line (' $' )
298- )
309+ local next_query = fn .search (
310+ ' GET\\ |POST\\ |PUT\\ |PATCH\\ |DELETE' ,
311+ ' n' ,
312+ fn .line (' $' )
313+ )
299314 next_query = next_query > 1 and next_query or fn .line (' $' )
300315
301316 local headers = get_headers (bufnr , last_query_line_number )
@@ -316,7 +331,7 @@ local function run()
316331 local auth = get_auth (bufnr , last_query_line_number )
317332 local accept = get_accept (bufnr , last_query_line_number )
318333
319- curl_cmd ( {
334+ local success_req = pcall ( curl_cmd , {
320335 method = parsed_url .method :lower (),
321336 url = parsed_url .url ,
322337 headers = headers ,
@@ -325,6 +340,12 @@ local function run()
325340 auth = auth ,
326341 })
327342
343+ if not success_req then
344+ error (
345+ ' [rest.nvim] Failed to perform the request.\n Make sure that you have entered the proper URL and the server is running.' ,
346+ 2
347+ )
348+ end
328349 go_to_line (bufnr , last_query_line_number )
329350end
330351
0 commit comments