Pastey db_err_handler.e

namespace err_handler
 
include GtkEngine.e 
include std/eds.e 
 
------------------------------- 
global type db_err(object x) -- trap eds errors in database procedures, shows popup message; 
------------------------------- 
if x != DB_OK then 
    display("Database Error: [] [] []",{x,decode_err(x),get("input_entry.text")}) 
    Error("MainWin",format("Database error code []",x), 
        format("DB: []",{get("input_entry.text")}), 
        format("[]",{decode_err(x)}),GTK_BUTTONS_OK) 
    return 1 
end if 
return x = DB_OK 
end type 
 
-------------------------------- 
global type tbl_err(object x) -- trap eds errors in table procedures ... ; 
-------------------------------- 
if x != DB_OK then 
    display("Table Error: [] [] [] ",{x,decode_err(x),get("input_entry.text")}) 
    Error("MainWin",format("Table error code []",x), 
        get("input_entry.text")) 
    return 1 
end if 
return x = DB_OK 
end type 
 
-------------------------------- 
global type rec_err(object x) -- trap eds errors in record procedures ... ; 
-------------------------------- 
if x != DB_OK then 
    display("Record error code [] [] []",{x,decode_err(x),get("input_entry.text")}) 
    Error("MainWin",format("Record Error code []",x), 
        get("input_entry.text")) 
    return 1 
end if 
return x = DB_OK 
end type 
 
----------------------------------------- 
global procedure ShowError(object msg) -- display errors from eds; 
----------------------------------------- 
 display(msg) -- on terminal if open; 
 display(db_get_errors()) -- on terminal if open; 
 Warn("MainWin","ShowError","Database error",msg) 
end procedure 
 
-------------------------- 
global function dbase() -- assure a database is open; 
-------------------------- 
if length(db_current()) < 1 then 
    Warn("MainWin","utils:dbase","No database is open!") 
    return 0 
end if 
return 1 
end function 
 
-------------------------- 
global function table() -- assure a table is selected; 
-------------------------- 
if length(db_current()) < 1 then 
    Warn("MainWin","utils:table","No database is open!") 
    return 0 
end if 
if length(db_current_table()) < 1 then 
    Warn("MainWin",,"No table is selected!") 
    return 0 
end if 
return 1 
end function 
 
--------------------------------- 
global function UpdateStatus() -- display db name and table name on window header; 
--------------------------------- 
object db = filename(db_current()) 
object tbl = "" 
if length(db) > 0 then 
    tbl = db_current_table()    
end if 
set("MainWin","title",format("DB: [] Table: []",{db,tbl})) 
return 1 
end function 
 
--------------------------------- 
function decode_err(integer x) -- convert error codes to human-readable form; 
--------------------------------- 
switch x do 
    case DB_OPEN_FAIL then return "open failed" 
    case DB_EXISTS_ALREADY then return "exists already" 
    case DB_LOCK_FAIL then return "lock failed" 
    case DB_BAD_NAME then return "bad name" 
    case DB_FATAL_FAIL then return "fatal error" 
end switch 
return "unknown error" 
end function 

1. Comment by irv Oct 15, 2020

This is a tricky one. Not only is it nearly 100 lines of code, but it uses type() to display errors returned by eds db, db_table, and db_record functions. It also provides a display of errors from eds procedures.

functions dbase() and table() handle problems like trying to add a table or a record when no database is selected, and give a visual report rather than just crashing.