fix: company save 500 error — catch DB exceptions, show error to user

- CompanyRepository::save() now catches PDOException and throws a
  RuntimeException with a human-readable message (duplicate name, etc.)
- Also fixes unchecked checkboxes (requires_approval, is_active) not
  being saved as 0 on UPDATE
- index.php save handler catches RuntimeException and redirects back to
  the form with an ?error= param instead of crashing with 500
- companyEdit() renders the error banner when ?error= is present

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-06-28 18:05:38 -05:00
co-authored by Claude Sonnet 4.6
parent 68317da748
commit b25126e52b
3 changed files with 51 additions and 20 deletions
+11 -6
View File
@@ -241,12 +241,17 @@ $routes = [
// ─── Guardar empresa (crear/actualizar) ─────────────────────────────────
['POST', '/admin/company/save', fn() => (function () {
SessionAuth::require();
$data = $_POST;
$id = CompanyRepository::save($data);
if ($id > 0) {
header('Location: /admin/companies?msg=' . ($data['id'] ?? 0 > 0 ? 'updated' : 'created'));
} else {
header('Location: /admin/companies?msg=error');
$data = $_POST;
$isNew = empty($data['id']) || (int)$data['id'] === 0;
try {
CompanyRepository::save($data);
header('Location: /admin/companies?msg=' . ($isNew ? 'created' : 'updated'));
} catch (\RuntimeException $e) {
$errMsg = urlencode($e->getMessage());
$redirect = $isNew
? '/admin/companies/new?error=' . $errMsg
: '/admin/company/edit?id=' . (int)$data['id'] . '&error=' . $errMsg;
header('Location: ' . $redirect);
}
exit;
})()],