Skip to content

Instantly share code, notes, and snippets.

@Muhammad-Haris-2
Created March 23, 2025 11:31
Show Gist options
  • Select an option

  • Save Muhammad-Haris-2/a1e8f1e7ed892fecf64f1eb0157125ea to your computer and use it in GitHub Desktop.

Select an option

Save Muhammad-Haris-2/a1e8f1e7ed892fecf64f1eb0157125ea to your computer and use it in GitHub Desktop.
import 'dart:developer';
import 'dart:io';
import 'dart:async';
class APIException implements Exception {
final String? message;
final int? statusCode;
final Exception? exception;
APIException({this.message, this.statusCode, this.exception});
@override
String toString() {
log("Exception: $exception, Status Code: $statusCode, Message: $message");
if (exception is APIException) {
return exception.toString();
} else if (exception is SocketException) {
return "No internet connection available. Please check your connection and try again.";
} else if (exception is TimeoutException) {
return "The request is taking too long. Please check your internet connection and try again.";
} else if (statusCode != null) {
return _handleHttpStatus(statusCode!);
} else if (message != null) {
return message!;
} else {
return "An unexpected error occurred.";
}
}
String _handleHttpStatus(int code) {
switch (code) {
case 100:
return "Continue: The server received the request headers, the client should proceed to send the request body.";
case 101:
return "Switching Protocols: The requester has asked the server to switch protocols.";
case 102:
return "Processing: The server has received and is processing the request.";
case 103:
return "Early Hints: The server is sending preliminary response headers.";
case 200:
return "OK: The request was successful.";
case 201:
return "Created: The request was successful and a new resource was created.";
case 202:
return "Accepted: The request has been accepted but is not yet processed.";
case 203:
return "Non-Authoritative Information: The response is from a third-party source.";
case 204:
return "No Content: The request was successful but there is no content.";
case 205:
return "Reset Content: The request was successful, reset the view.";
case 206:
return "Partial Content: The server is delivering partial content.";
case 207:
return "Multi-Status: The response contains multiple status codes.";
case 208:
return "Already Reported: The resource has already been reported.";
case 226:
return "IM Used: The server has fulfilled the request with instance manipulations.";
case 300:
return "Multiple Choices: There are multiple options for the resource.";
case 301:
return "Moved Permanently: The resource has been moved to a new URL permanently.";
case 302:
return "Found: The resource is temporarily under a different URL.";
case 303:
return "See Other: The resource is found at a different URL.";
case 304:
return "Not Modified: The resource has not been modified since last request.";
case 305:
return "Use Proxy: The resource is only accessible through a proxy.";
case 307:
return "Temporary Redirect: The resource is temporarily redirected.";
case 308:
return "Permanent Redirect: The resource is permanently redirected.";
case 400:
return "Bad Request: The request could not be understood due to malformed syntax.";
case 401:
return "Unauthorized: Authentication is required to access this resource.";
case 402:
return "Payment Required: Payment is required to access this resource.";
case 403:
return "Forbidden: You do not have permission to access this resource.";
case 404:
return "Not Found: The requested resource was not found.";
case 405:
return "Method Not Allowed: The request method is not supported.";
case 406:
return "Not Acceptable: The request is not acceptable.";
case 407:
return "Proxy Authentication Required: Authentication via proxy is required.";
case 408:
return "Request Timeout: The request took too long to complete.";
case 409:
return "Conflict: There is a conflict with the current state of the resource.";
case 410:
return "Gone: The requested resource is no longer available.";
case 411:
return "Length Required: The request did not specify content length.";
case 412:
return "Precondition Failed: The request precondition was not met.";
case 413:
return "Payload Too Large: The request payload is too large.";
case 414:
return "URI Too Long: The request URI is too long.";
case 415:
return "Unsupported Media Type: The server does not support the media format.";
case 416:
return "Range Not Satisfiable: The requested range is invalid.";
case 417:
return "Expectation Failed: The server cannot meet the expectation.";
case 418:
return "I'm a Teapot: A playful response indicating the server refuses to brew coffee.";
case 421:
return "Misdirected Request: The server cannot produce a response for this request.";
case 422:
return "Unprocessable Entity: The request was well-formed but could not be processed.";
case 423:
return "Locked: The resource is locked.";
case 424:
return "Failed Dependency: The request failed due to a dependent request.";
case 425:
return "Too Early: The request was rejected because it might be replayed.";
case 426:
return "Upgrade Required: The client must switch to a different protocol.";
case 428:
return "Precondition Required: The request must include conditions.";
case 429:
return "Too Many Requests: You have sent too many requests in a short time.";
case 431:
return "Request Header Fields Too Large: The headers are too large.";
case 451:
return "Unavailable For Legal Reasons: The resource is restricted by law.";
case 500:
return "Internal Server Error: The server encountered an unexpected condition.";
case 501:
return "Not Implemented: The server does not support the requested functionality.";
case 502:
return "Bad Gateway: The server received an invalid response from an upstream server.";
case 503:
return "Service Unavailable: The server is not ready to handle the request.";
case 504:
return "Gateway Timeout: The server did not receive a timely response.";
case 505:
return "HTTP Version Not Supported: The server does not support the HTTP version.";
default:
return "An unknown error occurred (Error: $code).";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment