@@ -76,8 +76,12 @@ local function get_body(bufnr, stop_line, query_line, json_body)
76
76
if start_line > 0 then
77
77
local json_string = ' '
78
78
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
+ )
81
85
82
86
for _ , v in ipairs (json_lines ) do
83
87
json_string = json_string .. utils .replace_env_vars (v )
@@ -124,24 +128,28 @@ local function get_headers(bufnr, query_line)
124
128
break_loops = true
125
129
break
126
130
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
+ )
129
137
130
138
for _ , header in ipairs (get_header ) do
131
139
header = utils .split (header , ' :' )
132
140
if
133
141
header [1 ]:lower () ~= ' accept'
134
142
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
137
145
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
145
153
headers [header [1 ]:lower ()] = header [2 ]
146
154
end
147
155
end
@@ -166,8 +174,12 @@ local function get_accept(bufnr, query_line)
166
174
-- Case-insensitive search
167
175
local start_line = fn .search (' \\ cAccept:' , ' ' , stop_line )
168
176
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
+ )
171
183
172
184
for _ , accept_data in pairs (accept_line ) do
173
185
accept = utils .split (accept_data , ' :' )[2 ]
@@ -176,15 +188,15 @@ local function get_accept(bufnr, query_line)
176
188
177
189
go_to_line (bufnr , query_line )
178
190
179
- return accept
191
+ return accept
180
192
end
181
193
182
194
-- get_auth retrieves the HTTP Authorization and returns a lua table with its values
183
195
-- @param bufnr Buffer number, a.k.a id
184
196
-- @param query_line Line to set cursor position
185
197
local function get_auth (bufnr , query_line )
186
198
local auth = {}
187
- local auth_not_empty = false
199
+ local auth_not_empty = false
188
200
-- Set stop at end of bufer
189
201
local stop_line = fn .line (' $' )
190
202
@@ -193,8 +205,12 @@ local function get_auth(bufnr, query_line)
193
205
-- Case-insensitive search
194
206
local start_line = fn .search (' \\ cAuthorization:' , ' ' , stop_line )
195
207
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
+ )
198
214
199
215
for _ , auth_data in pairs (auth_line ) do
200
216
-- Split by spaces, e.g. {'Authorization:', 'user:pass'}
@@ -205,10 +221,10 @@ local function get_auth(bufnr, query_line)
205
221
end
206
222
207
223
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
212
228
end
213
229
214
230
-- curl_cmd runs curl with the passed options, gets or creates a new buffer
@@ -259,7 +275,7 @@ local function curl_cmd(opts)
259
275
--- Add the curl command results into the created buffer
260
276
if json_body then
261
277
-- format JSON body
262
- res .body = fn .system (" jq " , res .body )
278
+ res .body = fn .system (' jq ' , res .body )
263
279
end
264
280
local lines = utils .split (res .body , ' \n ' )
265
281
line_count = api .nvim_buf_line_count (res_bufnr ) - 1
@@ -290,12 +306,11 @@ local function run()
290
306
local parsed_url = parse_url (fn .getline (' .' ))
291
307
local last_query_line_number = fn .line (' .' )
292
308
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
+ )
299
314
next_query = next_query > 1 and next_query or fn .line (' $' )
300
315
301
316
local headers = get_headers (bufnr , last_query_line_number )
@@ -316,7 +331,7 @@ local function run()
316
331
local auth = get_auth (bufnr , last_query_line_number )
317
332
local accept = get_accept (bufnr , last_query_line_number )
318
333
319
- curl_cmd ( {
334
+ local success_req = pcall ( curl_cmd , {
320
335
method = parsed_url .method :lower (),
321
336
url = parsed_url .url ,
322
337
headers = headers ,
@@ -325,6 +340,12 @@ local function run()
325
340
auth = auth ,
326
341
})
327
342
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
328
349
go_to_line (bufnr , last_query_line_number )
329
350
end
330
351
0 commit comments