Skip to content

Commit

Permalink
feat(history): enhance error handling and display type determination
Browse files Browse the repository at this point in the history
Closed #226
  • Loading branch information
jellydn committed Jan 26, 2025
1 parent 86cfb41 commit 0653010
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 27 deletions.
35 changes: 21 additions & 14 deletions lua/hurl/history.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,31 @@ function M.show_last_response()
display.show(last_response, last_response.display_type or 'text')
end

-- Function to be called after each successful request
function M.update_history(response)
-- Function to be called after each request
--- Update the history with the response
---@param response table
---@param type? string
function M.update_history(response, type)
-- Ensure response_time is a number
response.response_time = tonumber(response.response_time) or '-'

-- Determine the content type and set display_type
local content_type = response.headers['Content-Type']
or response.headers['content-type']
or 'text/plain'

if content_type:find('json') then
response.display_type = 'json'
elseif content_type:find('html') then
response.display_type = 'html'
elseif content_type:find('xml') then
response.display_type = 'xml'
if type == 'error' then
response.display_type = 'shell'
else
response.display_type = 'text'
-- Determine the content type and set display_type
local content_type = response.headers['Content-Type']
or response.headers['content-type']
or 'text/plain'

if content_type:find('json') then
response.display_type = 'json'
elseif content_type:find('html') then
response.display_type = 'html'
elseif content_type:find('xml') then
response.display_type = 'xml'
else
response.display_type = 'text'
end
end

add_to_history(response)
Expand Down
52 changes: 40 additions & 12 deletions lua/hurl/lib/hurl_runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,23 @@ function M.run_hurl_verbose(filePath, fromEntry, toEntry, isVeryVerbose, additio
local end_time = vim.loop.hrtime()
local response_time = (end_time - start_time) / 1e6 -- Convert to milliseconds

local display_data = {
headers = {},
body = stderr_data,
response_time = response_time,
status = code,
url = filePath,
method = 'N/A',
curl_command = 'N/A',
hurl_command = hurl_command,
captures = {},
timings = {},
}

if code ~= 0 then
utils.log_info('Hurl command failed with code ' .. code)
display.show({ body = '# Hurl Error\n\n```sh\n' .. stderr_data .. '\n```' }, 'markdown')
history.update_history(display_data, 'error')
return
end

Expand Down Expand Up @@ -202,7 +216,7 @@ function M.run_hurl_verbose(filePath, fromEntry, toEntry, isVeryVerbose, additio
table.insert(output_lines, '---')

-- Update history for each entry
local display_data = {
local entry_display_data = {
headers = entry.response.headers,
body = entry.response.body,
response_time = response_time,
Expand All @@ -214,7 +228,7 @@ function M.run_hurl_verbose(filePath, fromEntry, toEntry, isVeryVerbose, additio
captures = entry.captures,
timings = entry.timings,
}
history.update_history(display_data)
history.update_history(entry_display_data)

-- Save captures as global variables
save_captures_as_globals(entry.captures)
Expand Down Expand Up @@ -323,6 +337,19 @@ function M.execute_hurl_cmd(opts, callback)
M.is_running = false
spinner.hide()

local display_data = {
headers = {},
body = stderr_data,
response_time = (vim.loop.hrtime() - M.start_time) / 1e6, -- Convert to milliseconds
status = code,
url = 'N/A',
method = 'N/A',
curl_command = 'N/A',
hurl_command = table.concat(cmd, ' '),
captures = {},
timings = {},
}

if code ~= 0 then
utils.log_error('Hurl command failed with code ' .. code)
utils.notify('Hurl command failed. Check the split view for details.', vim.log.levels.ERROR)
Expand All @@ -339,6 +366,7 @@ function M.execute_hurl_cmd(opts, callback)
curl_command = 'N/A',
}
split.show(error_data, 'markdown')
history.update_history(display_data, 'error')
return
end

Expand All @@ -364,7 +392,7 @@ function M.execute_hurl_cmd(opts, callback)

-- Prepare the data for display
local last_entry = result.entries[#result.entries]
local display_data = {
local entry_display_data = {
headers = last_entry.response.headers,
body = last_entry.response.body,
response_time = M.response.response_time,
Expand All @@ -375,23 +403,23 @@ function M.execute_hurl_cmd(opts, callback)
}

-- Separate headers from body
local body_start = display_data.body:find('\n\n')
local body_start = entry_display_data.body:find('\n\n')
if body_start then
local headers_str = display_data.body:sub(1, body_start - 1)
display_data.body = display_data.body:sub(body_start + 2)
local headers_str = entry_display_data.body:sub(1, body_start - 1)
entry_display_data.body = entry_display_data.body:sub(body_start + 2)

-- Parse additional headers from the body
for header in headers_str:gmatch('([^\n]+)') do
local key, value = header:match('([^:]+):%s*(.*)')
if key and value then
display_data.headers[key] = value
entry_display_data.headers[key] = value
end
end
end

-- Determine the content type
local content_type = display_data.headers['Content-Type']
or display_data.headers['content-type']
local content_type = entry_display_data.headers['Content-Type']
or entry_display_data.headers['content-type']
or 'text/plain'

local display_type = 'text'
Expand All @@ -403,11 +431,11 @@ function M.execute_hurl_cmd(opts, callback)
display_type = 'xml'
end

display_data.display_type = display_type
entry_display_data.display_type = display_type

container.show(display_data, display_type)
container.show(entry_display_data, display_type)

history.update_history(display_data)
history.update_history(entry_display_data)

-- Save captures as global variables
if result.entries and #result.entries > 0 then
Expand Down
2 changes: 1 addition & 1 deletion lua/hurl/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ function M.setup()
utils.notify('Failed to load display module: ' .. display, vim.log.levels.ERROR)
return
end
display.show(last_response, 'json')
display.show(last_response, last_response.display_type or 'json')
else
utils.notify('No response history available', vim.log.levels.INFO)
end
Expand Down

0 comments on commit 0653010

Please # to comment.