Documentation

On this page

Download full book (text)

# **Chapter 19: Debugging and Error Tracing — debugMode, Edit Mode, and the Error Log** > **Prerequisites:** Chapters 1-5 > **Try it:** Tested code from gate `ex16debug` > (`apps/examples/Ex16debug.gate.php` + `apps/examples/fronts/ex16debug_main.front`). > Open `http://localhost/SartajPhpDoc/ex16debug.html`. --- ## **19.1 The Four Error Channels** SartajPhp separates *user-facing* problems from *developer-facing* ones, and *soft* failures from *hard* crashes: | Channel | Set by | Seen by | |---------|--------|---------| | Soft error | `setErr('code', 'msg')` | User (Master `traceError`) | | Inner error | `setErrInner('code', 'detail')` | Developer (Master `traceErrorInner`, debugMode) | | Console trace | `debugMode > 0`, `$this->debug->println()` | Browser DevTools console | | Hard failure | Exception / fatal | Error page + `cache/Sphp_errorLog.txt` | The single probe for "did anything soft fail?" is `getCheckErr()` — every chapter's insert/update flow already checks it. ## **19.2 debugMode (comp.php)** ```php $debugMode = 2; // 0=off, 1=errors, 2=warnings, 3=verbose ``` With `debugMode > 0` the framework appends `console.info` traces to the page: Gate file, event, event parameter, FrontFile path, and timing — e.g. (real output from this project's log): ``` Gate:- blog :Registered-File: .../apps/examples/Blog.gate.php :Event: :Event-Parameter: :URL: blog.html FrontFile Run:- .../apps/examples/fronts/blog_list.front SPHP Time(ms):- 7.36, Load Time:- 10.53, TT:- 18.4 ``` Your own lines go to the same console: ```php <?php $this->debug->println('ex16debug page_new — check browser console'); ``` ## **19.3 Soft Errors: setErr + setErrInner (tested)** ```php <?php /** URL: ex16debug-soft.html — soft/logical error, PHP keeps running */ public function page_event_soft($evtp) { SphpBase::sphpApi()->setErr('ex16soft', 'Soft error demo: required value missing (user-facing).'); SphpBase::sphpApi()->setErrInner('ex16soft', 'Fix in apps/examples/Ex16debug.gate.php → page_event_soft (ignore vendor/sartajphp frames).'); $this->debug->println('soft error set; getCheckErr=' . (SphpBase::sphpApi()->getCheckErr() ? 'true' : 'false')); $this->bindDebugPanel(); $this->setFrontFile($this->frtMain); } ``` Pattern: `setErr` = what the user may see ("required value missing"), `setErrInner` = what the developer needs ("which file, which rule"). The Master renders them via `##{raw:traceError(true)}#` and `traceErrorInner(true)` — every Front in this book includes the user line. ## **19.4 Hard Errors: Exceptions and the Error Log** ```php <?php /** URL: ex16debug-boom.html — intentional hard failure */ public function page_event_boom($evtp) { throw new \RuntimeException( 'Intentional coding error in apps/examples/Ex16debug.gate.php page_event_boom — ' . 'read stack: skip vendor/sartajphp; open the first apps/ or masters/ or fronts/ frame.' ); } ``` Hard failures land in **`cache/Sphp_errorLog.txt`** with URL + context. Real entry from this project (trimmed): ``` [2026-11-28: 16:14:51] URL:- /SartajPhpDoc/product.html SartajPHP Fatel Error:- Type:-2 Msg:-Undefined array key "news" File:-...Pagination.php on line no. 215 ``` **The stack-reading rule (the one habit that matters):** the framework core is ionCube-encoded and correct until proven otherwise. When reading any trace, **skip every `vendor/sartajphp/` frame and fix the first `apps/`, `fronts/`, or `masters/` frame.** In the entry above the crash line is inside `Pagination.php` — but the *bug* was a custom dbEngine returning the wrong `fetchQuery` shape from `apps/`/`engines/` (Chapter 12's pitfall list). ## **19.5 Edit Mode: See Which Front and Line Rendered a Tag** `blnEditMode` (settings) or a **session token** turns on HTML source markers — and the session form is live-safe: only your browser sees them. ``` http://localhost/SartajPhpDoc/ex16debug.html?edtmode=abc ← enables for YOUR session ``` With edit mode ON, rendered tags carry: | Attribute | Meaning | |-----------|---------| | `data-sedt` | Front file that produced the tag | | `data-sedtl` | Line number in that Front | | `data-sedtt` | Character offset | Plus meta tags naming the Master and Front wrappers. Dynamically generated HTML (jsServer blocks, `addJsonFront` payloads) has no markers — which is itself a diagnostic: *no `data-sedt` = not from a static Front line.* The tested panel reads the current state: ```php <?php private function bindDebugPanel() { global $debugMode; $this->dbgmode = (string) (int) $debugMode; $this->editmode = SphpBase::sphpSettings()->blnEditMode ? 'ON' : 'OFF'; $ses = (string) SphpBase::sphpRequest()->session('edtmode'); $this->edtsession = $ses !== '' ? 'set' : 'empty'; $this->checkerr = SphpBase::sphpApi()->getCheckErr() ? 'true' : 'false'; } ``` ## **19.6 App Notices vs Framework Frames** ```php <?php /** URL: ex16debug-code.html — intentional app-level E_USER_NOTICE */ public function page_event_code($evtp) { require_once __DIR__ . '/ex16_buggy_helper.php'; ex16_trigger_app_notice('demo from Gate'); // helper does: trigger_error('...', E_USER_NOTICE) } ``` Notices appear in the console/log with the **app file** named — same skip- vendor rule applies. Fix `apps/examples/ex16_buggy_helper.php`, not the framework wrapper that reported it. ## **19.7 A Debugging Routine That Works** 1. **Wrong output?** → open DevTools console (debugMode > 0): which Gate, which event, which Front ran? Wrong Front = wrong `setFrontFile` branch. 2. **Form silently not saving?** → print `getCheckErr()`; a soft error is set. Find it via `traceError`/`traceErrorInner` output. 3. **Blank/error page?** → tail `cache/Sphp_errorLog.txt`; first non-vendor frame is your bug. 4. **Can't tell which template made this HTML?** → `?edtmode=...` session token, inspect `data-sedt*`. 5. **DB says "Couldn't Execute Query"?** → inner error carries the SQL; check the value escaping (`cleanQuery`) and the engine contract (Chapter 12). 6. **Works on GET, fails on POST?** → recID/`isUpdateRequest` flow (Chapters 12, 15) — the classic missing `setRecID` bug. ## **19.8 Verify It Yourself** Against `http://localhost/SartajPhpDoc/`: 1. `ex16debug.html` → panel shows debugMode/editMode/getCheckErr; console has the `println` line 2. `ex16debug.html?edtmode=abc` → reload page → inspect any element for `data-sedt` markers 3. `ex16debug-soft.html` → red user error on page, `getCheckErr=true` 4. `ex16debug-code.html` → app notice in console/log naming the app helper 5. `ex16debug-boom.html` → error page; `cache/Sphp_errorLog.txt` gains an entry naming `Ex16debug.gate.php` --- **Previous:** [Chapter 18 — AutoGate](chapter18.md) · **Next:** [Chapter 20 — Output Cache](chapter20.md)