Browse Source

refact: ditch try/catch in favour of pcall

my-config
NTBBloodbath 3 years ago
parent
commit
1eaaed622f
No known key found for this signature in database GPG Key ID: 18D6730BC846AAC5
  1. 23
      lua/doom/core/config/doomrc.lua
  2. 21
      lua/doom/core/config/ui.lua
  3. 69
      lua/doom/core/functions/init.lua
  4. 14
      lua/doom/utils/init.lua

23
lua/doom/core/config/doomrc.lua

@ -16,7 +16,7 @@ log.debug('Loading Doom doomrc module ...')
M.check_doomrc = function()
-- /home/user/.config/doom-nvim/doomrc
log.debug('Looking for a doomrc file in Doom root ...')
if vim.fn.filereadable(utils.doom_root .. '/doomrc') then
if vim.fn.filereadable(utils.doom_root .. '/doomrc') == 1 then
return true
end
@ -220,18 +220,15 @@ end
-- default configs
M.load_doomrc = function()
-- /home/user/.config/doom-nvim/doomrc
if vim.fn.filereadable(utils.doom_root .. '/doomrc') then
utils.try({
function()
log.debug('Loading the doomrc ...')
vim.cmd('luafile ' .. utils.doom_root .. '/doomrc')
end,
utils.catch({
function(_)
log.error('Error while loading the doomrc')
end,
}),
})
if vim.fn.filereadable(utils.doom_root .. '/doomrc') == 1 then
local loaded_doomrc, err = pcall(function()
log.debug('Loading the doomrc ...')
vim.cmd('luafile ' .. utils.doom_root .. '/doomrc')
end)
if not loaded_doomrc then
log.error('Error while loading the doomrc. Traceback:\n' .. err)
end
else
log.warn('No doomrc file found, falling to defaults')
default_doomrc_values()

21
lua/doom/core/config/ui.lua

@ -11,18 +11,15 @@ log.debug('Loading Doom UI module ...')
-- If no colorscheme was established then fallback to defauls
if not utils.is_empty(Doom.colorscheme) then
utils.try({
function()
vim.opt.background = Doom.colorscheme_bg
vim.api.nvim_command('colorscheme ' .. Doom.colorscheme)
end,
utils.catch({
function(_)
log.error('Colorscheme not found, falling to doom-one')
vim.api.nvim_command('colorscheme ' .. Doom.colorscheme)
end,
}),
})
local loaded_colorscheme = pcall(function()
vim.opt.background = Doom.colorscheme_bg
vim.api.nvim_command('colorscheme ' .. Doom.colorscheme)
end)
if not loaded_colorscheme then
log.error('Colorscheme not found, falling to doom-one')
vim.api.nvim_command('colorscheme ' .. Doom.colorscheme)
end
else
log.warn('Forced default Doom colorscheme')
vim.api.nvim_command('colorscheme doom-one')

69
lua/doom/core/functions/init.lua

@ -63,27 +63,24 @@ end
-- @tparam bool write If doom should save before exiting
-- @tparam bool force If doom should force the exiting
M.quit_doom = function(write, force)
utils.try({
function()
log.info('Checking if the colorscheme was changed ...')
local target = vim.g.colors_name
if target ~= Doom.colorscheme then
vim.cmd(
'silent !sed -i "s/\''
.. Doom.colorscheme
.. "'/'"
.. target
.. '\'/" $HOME/.config/doom-nvim/doomrc'
)
log.info('Colorscheme successfully changed to ' .. target)
end
end,
utils.catch({
function(err)
log.error('Unable to write to the doomrc. Traceback:\n' .. err)
end,
}),
})
local changed_colorscheme, err = pcall(function()
log.info('Checking if the colorscheme was changed ...')
local target = vim.g.colors_name
if target ~= Doom.colorscheme then
vim.cmd(
'silent !sed -i "s/\''
.. Doom.colorscheme
.. "'/'"
.. target
.. '\'/" $HOME/.config/doom-nvim/doomrc'
)
log.info('Colorscheme successfully changed to ' .. target)
end
end)
if not changed_colorscheme then
log.error('Unable to write to the doomrc. Traceback:\n' .. err)
end
local quit_cmd = ''
@ -104,17 +101,14 @@ end
-- check_updates checks for plugins updates
M.check_updates = function()
utils.try({
function()
local updated_plugins, err = pcall(function()
log.info('Updating the outdated plugins ...')
vim.cmd('PackerSync')
end,
utils.catch({
function(err)
log.error('Unable to update plugins. Traceback:\n' .. err)
end,
}),
})
end)
if not updated_plugins then
log.error('Unable to update plugins. Traceback:\n' .. err)
end
end
-- create_report creates a markdown report. It's meant to be used when a bug
@ -122,8 +116,7 @@ end
M.create_report = function()
local date = os.date('%Y-%m-%d %H:%M:%S')
utils.try({
function()
local created_report, err = pcall(function()
vim.cmd(
'silent !echo "'
.. vim.fn.fnameescape('#')
@ -155,13 +148,11 @@ M.create_report = function()
.. utils.doom_report
)
log.info('Report created at ' .. utils.doom_report)
end,
utils.catch({
function(err)
log.error('Error while writing report. Traceback:\n' .. err)
end,
}),
})
end)
if not created_report then
log.error('Error while writing report. Traceback:\n' .. err)
end
end
return M

14
lua/doom/utils/init.lua

@ -60,20 +60,6 @@ M.has_value = function(tabl, val)
return false
end
-- try/catch statements, see
-- https://gist.github.com/cwarden/1207556
M.catch = function(err)
return err[1]
end
M.try = function(block)
local status, result = pcall(block[1])
if not status then
block[2](result)
end
return result
end
-- Get current OS, returns 'Other' if the current OS is not recognized
M.get_os = function()
--[[

Loading…
Cancel
Save