Skip to content
Back to skills

Common Error Handling

ASecurity

Assumption: TypeScript API with a global exception handler. Move the Postgres boundary into the infrastructure layer, replace sensitive details, and let the API layer map the safe error globally. ```ts // infrastructure/orders-repository.ts export class DatabaseOperationError extends Error { readonly code = 'DATABASE_OPERATION_FAILED'; constructor(message: string, options?: { cause?: unknown }) { super(message, options); this.name = 'DatabaseOperationError'; } } async function saveOrder(order...

  • 584 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added September 5, 2026
developmenttypescriptsqlapidatabase

Works with

  • api

Security analysis

A100/100

Pro scans all 11 files and shows the line behind each finding

Scanned September 5, 2026

npx -y skills add HoangNguyen0403/agent-skills-standard --skill common-error-handling --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Common Error Handling?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Common Error Handling
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/hoangnguyen0403-common-error-handling-agent-skills-standard/badge)](https://www.skillsdirectory.com/skills/hoangnguyen0403-common-error-handling-agent-skills-standard)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
Assumption: TypeScript API with a global exception handler.

Move the Postgres boundary into the infrastructure layer, replace sensitive details, and let the API layer map the safe error globally.

```ts
// infrastructure/orders-repository.ts
export class DatabaseOperationError extends Error {
  readonly code = 'DATABASE_OPERATION_FAILED';

  constructor(message: string, options?: { cause?: unknown }) {
    super(message, options);
    this.name = 'DatabaseOperationError';
  }
}

async function saveOrder(order: Order): Promise<void> {
  try {
    await db.query(/* ... */);
  } catch (error) {
    logger.error({ err: error }, 'Failed to save order'); // retain diagnostics internally

    throw new DatabaseOperationError(
      'The database operation could not be completed.',
      { cause: error },
    );
  }
}
```

Remove the controller’s raw error response:

```ts
// controller
try {
  return await service.createOrder(input);
} catch (error) {
  throw error; // or omit the catch entirely
}
```

Map errors at the API boundary:

```ts
// global exception filter / middleware
function handleError(error: unknown, res: Response): void {
  if (error instanceof ValidationError) {
    res.status(400).json({
      error: {
        code: 'VALIDATION_FAILED',
        message: error.message,
      },
    });
    return;
  }

  if (error instanceof DatabaseOperationError) {
    res.status(500).json({
      error: {
        code: error.code,
        message: 'An internal error occurred.',
      },
    });
    return;
  }

  logger.error({ err: error }, 'Unhandled API error');

  res.status(500).json({
    error: {
      code: 'INTERNAL_ERROR',
      message: 'An internal error occurred.',
    },
  });
}
```

Keep domain errors pure business errors—never attach HTTP status codes there. Do not expose Postgres messages, SQL, connection details, or stack traces. Do not swallow errors: every `catch` must log, wrap, rethrow, or deliberately map the failure.

Files in this skill

  • eval-1.baseline.md622 B
  • eval-1.with-skill.md1.9 KB
  • eval-2.baseline.md3.4 KB
  • eval-2.with-skill.md4.5 KB
  • eval-3.baseline.md704 B
  • eval-3.with-skill.md1.1 KB
  • trigger-1.md144 B
  • trigger-2.md162 B
  • trigger-3.md165 B
  • trigger-4.md186 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.