// Copyright (c) 2026 Charles KWON OhJun (charleskwonohjun@gmail.com) // All rights reserved. // Error handling functions: ERRORBLOCK, ERRORNEW, DOSERROR, FERROR // Harbour error system: Error object + ErrorBlock callback chain. package hbrtl import ( "five/hbrt" ) var ( errorBlock hbrt.Value // current error handler block lastDosErr int // last OS error code lastFErr int // last file error code ) // ERRORBLOCK([bNewBlock]) → bOldBlock // Gets/sets the error handler code block. func ErrorBlock(t *hbrt.Thread) { nParams := t.ParamCount() t.Frame(nParams, 0) defer t.EndProc() old := errorBlock if old.IsNil() { old = hbrt.MakeNil() } if nParams >= 1 && !t.Local(1).IsNil() { errorBlock = t.Local(1) } t.RetVal(old) } // ERRORNEW() → oError // Creates a new Error object as a hash with standard Harbour error properties. func ErrorNew(t *hbrt.Thread) { t.Frame(0, 0) defer t.EndProc() keys := []hbrt.Value{ hbrt.MakeString("ARGS"), hbrt.MakeString("CANDEFAULT"), hbrt.MakeString("CANRETRY"), hbrt.MakeString("CANSUBSTITUTE"), hbrt.MakeString("CARGO"), hbrt.MakeString("DESCRIPTION"), hbrt.MakeString("FILENAME"), hbrt.MakeString("GENCODE"), hbrt.MakeString("OPERATION"), hbrt.MakeString("OSCODE"), hbrt.MakeString("SEVERITY"), hbrt.MakeString("SUBCODE"), hbrt.MakeString("SUBSYSTEM"), hbrt.MakeString("TRIES"), } vals := []hbrt.Value{ hbrt.MakeNil(), hbrt.MakeBool(false), hbrt.MakeBool(false), hbrt.MakeBool(false), hbrt.MakeNil(), hbrt.MakeString(""), hbrt.MakeString(""), hbrt.MakeInt(0), hbrt.MakeString(""), hbrt.MakeInt(0), hbrt.MakeInt(2), hbrt.MakeInt(0), hbrt.MakeString(""), hbrt.MakeInt(0), } h := &hbrt.HbHash{Keys: keys, Values: vals} order := make([]int, len(keys)) for i := range order { order[i] = i } h.Order = order t.RetVal(hbrt.MakeHashFrom(h)) } // DOSERROR([nNewCode]) → nOldCode func DosError(t *hbrt.Thread) { nParams := t.ParamCount() t.Frame(nParams, 0) defer t.EndProc() old := lastDosErr if nParams >= 1 && !t.Local(1).IsNil() { lastDosErr = t.Local(1).AsInt() } t.RetInt(int64(old)) } // FERROR() → nLastFileError func FError(t *hbrt.Thread) { t.Frame(0, 0) defer t.EndProc() t.RetInt(int64(lastFErr)) } // SetFError sets the file error code (called internally by file I/O functions). func SetFError(code int) { lastFErr = code }