*!*****************************************************************************
*!      Procedure: ERR_HAND
*!*****************************************************************************
PROCEDURE err_hand
PARAMETERS err_num, mssg, line_txt, mlineno
PRIVATE sta_debug, can_susp, lastw, old_errh, old_row, old_col, prog_chain, iii, otevreno
* takhle se aktivuje : ON ERROR DO err_hand WITH ERROR(), MESSAGE(), MESSAGE(1), LINE()

eh_old_sel = SELECT()
prog_chain = progchain("do predposledniho")

DO CASE 
CASE err_num=1526
    AERROR(chybapole)
    mssg=STR(m.err_num, 5) + ":" + chybapole(2)
    line_txt = STRTR(m.line_txt, "   ", " ")
    line_txt = STRTR(m.line_txt, "   ", " ")
    line_txt = ALLT(STRTR(m.line_txt, "  ", " "))
    line_txt = STR(m.mlineno, 5) + ":" + LEFT(m.line_txt, 222)
    line_txt=m.line_txt+" : "+chybapole(3)
    TRY 
        otevreno=IIF(ISNULL(chybapole(5)),"","ODBC error number:"+ALLTRIM(STR(chybapole(5))))+":"+IIF(ISNULL(chybapole(4)),"","SQL state:"+ALLTRIM(chybapole(4)))
    CATCH
        otevreno=""
    ENDTRY 

CASE err_num=1541    &&  Pripojení je obsazeno / Connection is busy.
    AERROR(chybapole)
    mssg=STR(m.err_num, 5) + ":" + chybapole(2) + chybapole(3)
    WAIT m.mssg WINDOW TIMEOUT .3
    line_txt=chybapole(3)
    TRY 
        otevreno=IIF(ISNULL(chybapole(5)),"-5-",ALLTRIM(STR(chybapole(5))))+":"+IIF(ISNULL(chybapole(4)),"","Prac oblast:"+ALLTRIM(STR(chybapole(4))))
    CATCH
        otevreno=""
    ENDTRY 
    DO app_log WITH m.line_txt
    POP KEY
    SELECT (m.eh_old_sel)
    RETRY

CASE ATC("poslimail", m.prog_chain) > 0 .and. ATC("smtp", m.line_txt) > 0              && chyba pri posilani mailu z hodne zabezpecenych siti
    DO app_log WITH m.line_txt
    RETURN 

OTHERWISE 

    mssg = STR(m.err_num, 5) + ":" + LEFT(m.mssg, 222)
    line_txt = STRTR(m.line_txt, "   ", " ")
    line_txt = STRTR(m.line_txt, "   ", " ")
    line_txt = ALLT(STRTR(m.line_txt, "  ", " "))
    line_txt = STR(m.mlineno, 5) + ":" + LEFT(m.line_txt, 222)
    TRY 
        otevreno = DBF() + IIF(EMPTY(DBF()), "", " as " + ALIAS() + IIF(EMPTY(ORDER()), " not ordered", " tag " + ORDER()))
        otevreno = SUBSTR(STRTR( m.otevreno, ".DBF", ""), 4)
    CATCH 
        otevreno=m.mssg
    ENDTRY 
ENDCASE 


DO CASE
CASE m.err_num = 1001    && function not available - kdyz se zapomene SUSPEND
    DO app_log WITH LEFT(m.line_txt,6) + " zapomenutej SUSPEND"
    RETURN

CASE m.err_num = 1190    && file too large
    DO app_log WITH m.line_txt
    MESSAGEBOX(jaz("form_data_prilis_velke")+CHR(13)+jaz("form_skuste_kratsie_ci_menej"),1+16, m.prog_chain)
    ON KEY
    DO del_pripath WITH m.gcpri_path
    DO shut_dn

CASE m.err_num = 1709 && Database object is being used by someone else
    gcansw = "X"
    WAIT "Database object used by someone else" WINDOW AT 22,22 TIMEOUT 1
    DO app_log WITH m.line_txt
    SELECT (m.eh_old_sel)
    RETRY
CASE m.err_num = 1526 AND ("connection dead"$LOWER(m.mssg) OR "connection lost"$LOWER(m.mssg) OR "state:08s01"$LOWER(m.otevreno))
    DO app_log WITH m.line_txt
    MESSAGEBOX(jaz("form_zruseny_connect_server"),64, "FoodMan")
    ON KEY
    DO del_pripath WITH m.gcpri_path
    DO shut_dn
OTHERWISE
    PUSH KEY CLEAR
    DEFINE POPUP pop_errh FROM 11,0 TITLE " "+PADR("Neocakavany stav",30) + m.gcverze + "  " + m.gchom_path + "  " + m.gcuser_id + "    " + m.machine + "    " + TTOC(DATETIME()) IN SCREEN
    DEFINE BAR  1 OF pop_errh PROMPT "Err : " + PADR(TRIM(m.mssg),      111)
    DEFINE BAR  2 OF pop_errh PROMPT "Line: " + PADR(TRIM(m.line_txt),  111)
    DEFINE BAR  3 OF pop_errh PROMPT "      " + PADR(SUBSTR(TRIM(m.line_txt),112), 111)
    DEFINE BAR  4 OF pop_errh PROMPT "Prog: " + PADR(TRIM(m.prog_chain),111)
    DEFINE BAR  5 OF pop_errh PROMPT "      " + PADR(SUBSTR(TRIM(m.prog_chain),112),111)
    DEFINE BAR  6 OF pop_errh PROMPT "Used: " + m.otevreno
    DEFINE BAR  7 OF pop_errh PROMPT "Pozn: " + m.err_h_info
    DEFINE BAR  8 OF pop_errh PROMPT "\-"
    DEFINE BAR  9 OF pop_errh PROMPT "\<Ignore . . . "+jaz("form_ignoruj_chybu")
    DEFINE BAR 10 OF pop_errh PROMPT "\<Retry. . . . "+jaz("form_opakuj_akci")
    DEFINE BAR 11 OF pop_errh PROMPT "\<Suspend. . . "+jaz("form_prerus_program") SKIP FOR !m.glis_admin
    DEFINE BAR 12 OF pop_errh PROMPT "\<Cancel . . . "+jaz("form_ukonci_program")
    DEFINE BAR 13 OF pop_errh PROMPT "\-"
    DEFINE BAR 14 OF pop_errh PROMPT STRTRAN(STRTRAN(ALLTRIM( _screen.Caption), "  ", " " ), "  ", " " )

    ON SELECTION POPUP pop_errh DEACTIVATE POPUP pop_errh
    ACTIVATE POPUP pop_errh BAR 12
    DO WHILE BAR() < 9
        ACTIVATE POPUP pop_errh BAR 12
    ENDDO
    RELEASE POPUP pop_errh
    gcansw = PROMPT()
    IF DATETIME()-m.poslednachybaodoslana>10
        IF !(".FXP"$UPPER(SYS(16,0)))
            LOCAL chybatext, textnazapisanie, zvysoktextu
            chybatext=TTOC(DATETIME())+" " + m.gcverze + " " + IIF(m.issql, "SQL:" + m.sqldatabaza + "/" + m.postgres_verze, m.gchom_path) + "  stred:" + m.gcpgm_id + "  user:" + m.gcuser_id + " " + m.machine+"<BR>"
            chybatext=m.chybatext+"Err : " + ALLTRIM(m.mssg)+"<BR>"
            chybatext=m.chybatext+"Line: " + ALLTRIM(m.line_txt)+"<BR>"
            chybatext=m.chybatext+"Prog: " + ALLTRIM(m.prog_chain)+"<BR>"
            chybatext=m.chybatext+"Used: " + ALLTRIM(m.otevreno)+"<BR>"
            chybatext=m.chybatext+"Pozn: " + ALLTRIM(m.err_h_info)+"<BR>"
            IF VARTYPE(m.chybapriukladanicursora)=="C" AND !EMPTY(m.chybapriukladanicursora)
                chybatext=m.chybatext+"<BR>"
                chybatext=m.chybatext+m.chybapriukladanicursora+"<BR>"
            ENDIF 
            DO poslimaildoalta WITH m.hotel_name, m.chybatext, ""
            poslednachybaodoslana = DATETIME()
        ENDIF 
    ENDIF 
    DO CASE
        CASE BAR() = 9    && Ignore
            DO app_log WITH m.line_txt
            POP KEY
            SELECT (m.eh_old_sel)
            RETURN
        CASE BAR() = 10    && Retry
            DO app_log WITH m.line_txt
            POP KEY
            SELECT (m.eh_old_sel)
            RETRY
        CASE BAR() = 11    && Suspend
            DO app_log WITH m.line_txt
            DO sta_susp
        OTHERWISE        && Cancel
            DO app_log WITH m.line_txt
            ON KEY
            DO del_pripath WITH m.gcpri_path
            DO shut_dn
    ENDCASE
ENDCASE
