Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- exception = { __tostring = function(e) return "ERROR: " .. e.msg end }
- function exception:new(msg)
- local error_table = {msg = msg}
- setmetatable(error_table, self)
- self.__index = self
- return error_table
- end
- function exception:newObj(error_table)
- setmetatable(error_table, self)
- self.__index = self
- return error_table
- end
- function try(f, catch)
- -- execute 'try' statement
- local status, exception = pcall(f)
- -- if an error occurred, execute 'catch' statement
- if not status then
- catch(exception)
- end
- end
- function coroutine_try(f, catch)
- -- execute 'try' statement but for coroutine
- local status, exception = copcall(f)
- -- if an error occurred, execute 'catch' statement
- if not status then
- catch(exception)
- end
- end
- function print_err(exception)
- -- save old color formatting
- local bg = term.getBackgroundColor()
- local fg = term.getTextColor()
- -- set colors
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.red)
- -- print error
- print(exception)
- -- restore colors
- term.setBackgroundColor(bg)
- term.setTextColor(fg)
- end
- function print_wrn(exception)
- --save old color formatting
- local bg = term.getBackgroundColor()
- local fg = term.getTextColor()
- -- set colors
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.yellow)
- -- print error
- print(exception)
- -- restore colors
- term.setBackgroundColor(bg)
- term.setTextColor(fg)
- end
- return {
- exception = exception,
- coroutine_try = coroutine_try,
- try = try,
- print_err = print_err,
- print_wrn = print_wrn
- }
Advertisement
Add Comment
Please, Sign In to add comment