Skip to content

Refactor: Consolidate error response handling across handlers #122

@melvincarvalho

Description

@melvincarvalho

Problem

Error responses are inconsistent across handlers:

// handlers/resource.js
reply.code(404).send({ error: 'Not Found' });

// handlers/container.js
reply.code(400).send({ error: 'Slug header too long' });

// Some include message field, some don't
reply.code(403).send({ error: 'Forbidden', message: 'Access denied' });
reply.code(404).send({ error: 'Not Found' }); // no message

// Some wrap in try/catch, some don't

Issues:

  1. Inconsistent error object shape (error vs error + message)
  2. Error strings not centralized (typo risk)
  3. No standard HTTP status code mapping
  4. Mixed try/catch patterns

Proposed Solution

Create error response utility:

// src/utils/errors.js

export const ErrorCodes = {
  NOT_FOUND: { code: 404, error: 'Not Found' },
  FORBIDDEN: { code: 403, error: 'Forbidden' },
  UNAUTHORIZED: { code: 401, error: 'Unauthorized' },
  BAD_REQUEST: { code: 400, error: 'Bad Request' },
  CONFLICT: { code: 409, error: 'Conflict' },
  PAYLOAD_TOO_LARGE: { code: 413, error: 'Payload Too Large' },
  UNSUPPORTED_MEDIA: { code: 415, error: 'Unsupported Media Type' },
  INTERNAL_ERROR: { code: 500, error: 'Internal Server Error' }
};

export function sendError(reply, errorType, message = '') {
  const { code, error } = errorType;
  return reply.code(code).send({ error, message });
}

Usage:

import { sendError, ErrorCodes } from '../utils/errors.js';

// Before
reply.code(404).send({ error: 'Not Found' });

// After
sendError(reply, ErrorCodes.NOT_FOUND, 'Resource does not exist');

Benefits

  • Consistent error shape across all handlers
  • Centralized error strings (no typos)
  • Easy to add logging/metrics
  • Simpler handler code

Files Affected

  • Create: src/utils/errors.js
  • Modify: src/handlers/resource.js
  • Modify: src/handlers/container.js
  • Modify: src/handlers/git.js
  • Modify: src/auth/middleware.js

Priority

P3 - Low priority polish

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: lowP3 - Do eventually, polishrefactorCode refactoring

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions