From 764899df5ca39076acb08a447f7e5bd0b4fa3147 Mon Sep 17 00:00:00 2001 From: Mathias Fussenegger Date: Sat, 24 Sep 2022 10:52:25 +0200 Subject: [PATCH] Fix nil body handling in fmt_error --- lua/dap/utils.lua | 2 +- tests/utils_spec.lua | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lua/dap/utils.lua b/lua/dap/utils.lua index ebf442ce..c2faae0c 100644 --- a/lua/dap/utils.lua +++ b/lua/dap/utils.lua @@ -4,7 +4,7 @@ local M = {} ---@param err ErrorResponse ---@return string? function M.fmt_error(err) - local body = err.body + local body = err.body or {} if body.error and body.error.showUser then local msg = body.error.format for key, val in pairs(body.error.variables or {}) do diff --git a/tests/utils_spec.lua b/tests/utils_spec.lua index 5c3c75aa..6452501c 100644 --- a/tests/utils_spec.lua +++ b/tests/utils_spec.lua @@ -1,3 +1,5 @@ +local utils = require('dap.utils') + describe('utils.index_of', function() it('returns index of first item where predicate matches', function() local result = require('dap.utils').index_of( @@ -79,4 +81,11 @@ describe('utils.fmt_error', function () } })) end) + + it('can handle response without body part', function() + local result = utils.fmt_error({ + message = 'Bad things happen', + }) + assert.are.same('Bad things happen', result) + end) end)