Himanshu SharmaSoftware engineer
Let’s talk

CodeIgniter 4 · Lesson 27 of 30

CodeIgniter diagnostics: useful errors without leaking secrets

Classify expected bookshop failures and write restrained diagnostics while keeping internal details and secrets out of public responses.

15 minConfidence and diagnosticsUpdated 14 November 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonClassify expected bookshop failures and write restrained diagnostics while keeping internal details and secrets out of public responses.

After this lesson you can separate expected application failures from internal faults and log a useful diagnostic without exposing secrets. Error handling is part of the response contract. A missing book should not look like a successful empty book, and an internal database failure should not reveal a query or filesystem path to a visitor.

Classify the failure first

The bookshop has several normal rejection paths. A title can fail validation. A numeric identifier can have no matching record. A caller can be denied an operation. These are expected outcomes that deserve deliberate responses. A broken database connection or a programming exception is a different class of problem.

text
Missing public book -> deliberate not-found response
Invalid submitted title -> validation response, no write
Denied operation -> rejection before side effects
Unexpected internal fault -> restrained public error, private diagnosis

This classification is a design map rather than captured output. It helps decide where to investigate. A title-policy failure should not trigger a migration reset, and a missing route should not be “fixed” by catching every exception in a view.

Keep useful context in logs

CodeIgniter provides log_message for severity-based logging. A small controller diagnostic can identify the operation without copying an entire request body. The following fragment passed PHP lint and was executed in the isolated application while verifying log behavior.

php
log_message('error', 'Bookshop catalogue read failed during a local diagnostic.');

This authored message contains no password, session identifier, token, connection string or private title. A real incident may need a correlation identifier and carefully chosen context. Design that context deliberately, and do not use a user's raw input as a log template.

The official Logging Information guide describes levels, thresholds and handlers. A call existing in code does not guarantee the configured threshold will write it to the destination you expect. Verify the configured handler and storage permissions in the actual environment. Avoid returning a log path as part of an error response.

Production and development differ

Detailed errors are useful in a restricted learning workspace. In public production responses they can disclose internal paths, table names and sensitive configuration. The environment setting affects diagnostics, but it does not replace a review of your own error handling and debug endpoints.

Never print all environment variables to investigate one missing setting. Never publish phpinfo as a permanent troubleshooting route. Similarly, do not leave a controller that intentionally throws an exception exposed after an experiment. Remove or deny temporary diagnostic routes when they no longer serve a controlled purpose.

The bookshop's missing-record exception is intentional. The framework converts it to a not-found HTTP response during ordinary server execution. In a feature-test lifecycle, the exception can be surfaced directly, which is why the written test expects it. An error observed in one lifecycle does not justify making unsupported claims about every other lifecycle.

Do not disguise failure as success

Catching every throwable and returning Book added would make the interface reassuring while losing data integrity. If persistence fails, the action must not claim acceptance. Preserve internal evidence and choose a response appropriate to the failed operation.

Retries also need care. A client unsure whether a write succeeded may submit again and create duplicates. The simple course form has no idempotency-key design. If you extend it to valuable operations, define how clients distinguish failure before persistence from uncertainty after persistence. That is a product and data-contract issue, not merely a nicer error paragraph.

Make logs operationally useful

A log that nobody can find or safely retain is a weak diagnostic tool. Decide who can read logs, how long they are retained and how disk growth is managed. Runtime logs belong outside public. Access should follow the host's filesystem model rather than broad permissions chosen to silence one warning.

Do not claim an error-level log is an alerting system. Alert delivery, escalation and incident ownership are separate operational choices. For this local course, verifying that an authored diagnostic reaches the configured private log is enough evidence for the demonstrated mechanism.

The fetched official Error Handling guide and Logging Information guide support the framework behavior discussed here. The failure classifications and bookshop message are original application examples.

Common mistakes

Returning success after a caught database exception lies about the operation. Logging complete requests can capture credentials and tokens. Treating every not-found outcome as an internal fault creates noisy diagnostics. Finally, exposing a development stack trace on a public host reveals details that belong in private troubleshooting evidence.

Exercise

Choose a response and diagnostic approach for an unknown book, an invalid title and an unavailable database. Explain why logging the entire request is unnecessary in all three cases and why a retry after an uncertain write can need special handling.

Show an answer

Use a deliberate not-found response for the unknown record and a validation response without persistence for invalid input. An unavailable database needs a restrained failure response and private operational context. Whole requests may contain secrets and add little diagnostic value. A write might already have persisted before a response was lost, so blindly repeating it can duplicate work.

Previous lesson · Next lesson · Related: explicit routes · Related: security boundaries

Key points

  • Expected rejection and unexpected internal failure need different handling.
  • Logging must be verified against the configured threshold and handler.
  • Never report successful persistence after swallowing an internal failure.

Check yourself

1. Should an unknown book be invented as an empty success?
2. Why avoid logging complete requests?
3. Does an error-level log automatically notify an operator?