Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handling utility #28

Closed
wants to merge 3 commits into from
Closed

Error handling utility #28

wants to merge 3 commits into from

Conversation

ishaan000
Copy link
Collaborator

@ishaan000 ishaan000 commented Jan 7, 2025

Adds a TypeScript utility for standardized API error handling that:

  • Handles success responses (2xx) with flash messages
  • Captures validation errors and info messages (4xx, 5xx)
  • Provides type-safe access to response data and error details
  • Includes status code tracking
  • Throws structured ApiError for consistent error handling

Example usage:

try {
  const response = await fetch('/api/endpoint');
  const data = await handleApiError<ResponseType>(response);
  // Success case
  console.log(data.flash?.success);
} catch (error) {
  if (error instanceof ApiError) {
    console.log(error.statusCode); // 400
    console.log(error.flash?.info); // ["Password must be at least 8 characters long."]
  }
}

- Define apiRequest function in api.ts with 'GET', 'POST', 'PUT', and 'DELETE' support.
- Add tests for api.ts
- Handles API response parsing
- Processes flash messages (success/error/info)
- Throws standardized ApiError with status code
- Includes Jest test
@ishaan000 ishaan000 added the feature Related to new feature label Jan 7, 2025
@ishaan000 ishaan000 requested a review from hiyaryan January 7, 2025 19:46
@ishaan000 ishaan000 self-assigned this Jan 7, 2025
@ishaan000 ishaan000 closed this Jan 7, 2025
@hiyaryan hiyaryan deleted the error-handling-utility branch January 23, 2025 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Related to new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant