# NGBVD Error Handling and Defensive Messages Implementation

## Purpose

This patch adds proper error handlers and error pages so the system returns clear, user-friendly messages instead of raw Laravel exceptions.

## Included

- `app/Exceptions/Handler.php` — central exception handling for web and JSON/API requests.
- `resources/views/errors/index.blade.php` — branded standalone error page.
- `resources/views/errors/403.blade.php` — access restriction page.
- `resources/views/errors/404.blade.php` — missing resource page.
- `resources/views/errors/419.blade.php` — session expired page.
- `resources/views/errors/422.blade.php` — data validation/data quality page.
- `resources/views/errors/500.blade.php` — system error page.
- `resources/views/errors/access.blade.php` — access-denied compatibility page.
- `resources/views/components/alerts.blade.php` — safe alert component that does not crash when `$errors` is missing.
- `app/Exceptions/DataQualityException.php` — exception for dirty or inconsistent data.
- `app/Services/Core/ColumnAwareLocationChainValidator.php` — prevents SQL errors when validating district/sub-county/parish chains.
- `app/Http/Controllers/Concerns/UsesColumnAwareLocationValidation.php` — optional trait for controllers.

## Current bug addressed

The uploaded log shows:

```text
Unknown column 'district_id' in 'where clause'
```

The fix avoids querying columns that do not exist in the installed database schema.

## Apply commands

```powershell
composer dump-autoload
php artisan optimize:clear
php artisan config:clear
php artisan route:clear
php artisan view:clear
php artisan cache:clear
```

## Testing

1. Submit `/dataentry/index?id=&tab=overview` with a valid District, Sub-County and Parish.
2. Try selecting a Sub-County that does not belong to the District. The system should show a clean data-quality message.
3. Open an invalid URL. The system should show a clean 404 page.
4. Try a restricted URL with a user that lacks permission. The system should show a clean 403/access restricted page.
5. Let a session expire and submit a form. The system should show a 419 session message.

## Important note

This patch does not alter the accepted UI/UX of normal system pages. It only adds error pages and defensive error handling.
