From f4f8c0eb6ce079992bf01ec554480902013f82c9 Mon Sep 17 00:00:00 2001 From: Marc Jakobi Date: Wed, 10 Jan 2024 22:06:59 +0100 Subject: [PATCH] fix(hoogle-web): error handling for invalid `curl` output --- CHANGELOG.md | 6 ++++++ lua/haskell-tools/hoogle/web.lua | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16848cb..de43a05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [3.1.2] - 2024-01-10 + +### Fixed + +- Hoogle (web): Error handling for invalid `curl` output [[#322](https://github.com/mrcjkb/haskell-tools.nvim/issues/322)]. + ## [3.1.1] - 2023-12-22 ### Fixed diff --git a/lua/haskell-tools/hoogle/web.lua b/lua/haskell-tools/hoogle/web.lua index a2dd404..2eb0a48 100644 --- a/lua/haskell-tools/hoogle/web.lua +++ b/lua/haskell-tools/hoogle/web.lua @@ -79,7 +79,9 @@ if deps.has_telescope() then opts.hoogle = opts.hoogle or {} opts.hoogle.json = true local url = mk_hoogle_request(search_term, opts) - compat.system({ 'curl', '--silent', url, '-H', 'Accept: application/json' }, nil, function(result) + local curl_command = { 'curl', '--silent', url, '-H', 'Accept: application/json' } + log.debug(curl_command) + compat.system(curl_command, nil, function(result) ---@cast result vim.SystemCompleted log.debug { 'Hoogle web response', result } local response = result.stdout @@ -87,8 +89,16 @@ if deps.has_telescope() then vim.notify('hoogle web: ' .. (result.stderr or 'error calling curl'), vim.log.levels.ERROR) return end - local results = vim.json.decode(response) + local ok, results = pcall(vim.json.decode, response) vim.schedule(function() + if not ok then + log.error { 'Hoogle web response (invalid JSON)', curl_command, 'result: ' .. result } + vim.notify( + "haskell-tools.hoogle: Received invalid JSON from curl. Likely due to a failed request. See ':HtLog' for details'", + vim.log.levels.ERROR + ) + return + end pickers .new(opts, { prompt_title = 'Hoogle: ' .. search_term,