-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22c184d
commit d72d090
Showing
10 changed files
with
129 additions
and
90 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:domain/entities/domain_exceptions.dart'; | ||
import 'package:http/http.dart'; | ||
|
||
/// Helper function to map server errors to domain-specific exceptions | ||
DomainException mapServerErrorToDomainException(Response response) { | ||
final statusCode = response.statusCode; | ||
final responseData = jsonDecode(response.body); | ||
final message = responseData['message']; | ||
|
||
switch (statusCode) { | ||
case 400: | ||
return BadRequestException(message ?? 'Bad Request'); | ||
case 401: | ||
return UnauthorizedException(message ?? 'Unauthorized'); | ||
case 403: | ||
return ForbiddenException(message ?? 'Forbidden'); | ||
case 404: | ||
return NotFoundException(message ?? 'Not Found'); | ||
case 409: | ||
return ResourceAlreadyExistsException(message ?? 'Resource already exists'); | ||
case 422: | ||
return UnprocessableEntityException(message ?? 'Unprocessable Entity'); | ||
case 429: | ||
return TooManyRequestsException(message ?? 'Too Many Requests'); | ||
case 500: | ||
return InternalServerErrorException(message ?? 'Internal Server Error'); | ||
default: | ||
return InternalServerErrorException(message ?? 'Server error with status code $statusCode'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// lib/domain/exceptions/domain_exceptions.dart | ||
abstract class DomainException implements Exception { | ||
final String message; | ||
DomainException(this.message); | ||
@override | ||
String toString() => '$runtimeType: $message'; | ||
} | ||
|
||
class BadRequestException extends DomainException { | ||
BadRequestException([String message = 'Bad Request']) : super(message); | ||
} | ||
|
||
class UnauthorizedException extends DomainException { | ||
UnauthorizedException([String message = 'Unauthorized']) : super(message); | ||
} | ||
|
||
class ForbiddenException extends DomainException { | ||
ForbiddenException([String message = 'Forbidden']) : super(message); | ||
} | ||
|
||
class NotFoundException extends DomainException { | ||
NotFoundException([String message = 'Not Found']) : super(message); | ||
} | ||
|
||
class ResourceAlreadyExistsException extends DomainException { | ||
ResourceAlreadyExistsException([String message = 'Resource already exists']) : super(message); | ||
} | ||
|
||
class UnprocessableEntityException extends DomainException { | ||
UnprocessableEntityException([String message = 'Unprocessable Entity']) : super(message); | ||
} | ||
|
||
class TooManyRequestsException extends DomainException { | ||
TooManyRequestsException([String message = 'Too Many Requests']) : super(message); | ||
} | ||
|
||
class InternalServerErrorException extends DomainException { | ||
InternalServerErrorException([String message = 'Internal Server Error']) : super(message); | ||
} | ||
|
||
// Add additional exceptions here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.