Skip to content

Instantly share code, notes, and snippets.

@samuchakraborty
Created May 16, 2022 11:46
Show Gist options
  • Save samuchakraborty/224a1161968cb9b0e7257b78460e44cf to your computer and use it in GitHub Desktop.
Save samuchakraborty/224a1161968cb9b0e7257b78460e44cf to your computer and use it in GitHub Desktop.
Fluuter APi Exception Handler
import 'dart:convert';
import 'package:http/http.dart'as http;
class AppException implements Exception {
final _message;
final _prefix;
AppException([this._message, this._prefix]);
String toString() {
return "$_prefix$_message";
}
}
class FetchDataException extends AppException {
FetchDataException([String? message])
: super(message, "Error During Communication: ");
}
class BadRequestException extends AppException {
BadRequestException([message]) : super(message, "Invalid Request: ");
}
class UnauthorisedException extends AppException {
UnauthorisedException([message]) : super(message, "Unauthorised: ");
}
class InvalidInputException extends AppException {
InvalidInputException([String? message]) : super(message, "Invalid Input: ");
}
import 'dart:io';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:async';
import 'package:project_i/app/exceptions/app_exceptions.dart';
class RemoteService {
final String _baseUrl = "http://techhuntbridge.com/api";
Future<dynamic> get(String url) async {
print('url $url');
try {
final response = await http.get(
Uri.parse(
_baseUrl + url,
),
headers: await _headers(),
);
return _returnResponse(response);
} on SocketException {
print('No Internet');
throw FetchDataException('No Internet connection');
}
}
Future<dynamic> post(String url, dynamic body) async {
print('url $url');
try {
final response = await http.post(
Uri.parse(
_baseUrl + url,
),
body: body,
headers: await _headers(),
);
return _returnResponse(response);
} on SocketException {
print('No Internet');
throw FetchDataException('No Internet connection');
}
}
Future<dynamic> put(String url, dynamic body) async {
print('url $url');
try {
final response = await http.put(
Uri.parse(
_baseUrl + url,
),
headers: await _headers(),
body: body,
);
return _returnResponse(response);
} on SocketException {
print('No Internet');
throw FetchDataException('No Internet connection');
}
}
Future<dynamic> delete(String url) async {
print('url $url');
try {
final response = await http.delete(
Uri.parse(
_baseUrl + url,
),
headers: await _headers(),
);
return _returnResponse(response);
} on SocketException {
print('No Internet');
throw FetchDataException('No Internet connection');
}
}
}
Future<Map<String, String>> _headers() async {
var token = ""; // get token from storage
return {"authorization": token, "accept": "application/json"};
}
dynamic _returnResponse(http.Response response) {
switch (response.statusCode) {
case 200:
var responseJson = json.decode(response.body.toString());
print(responseJson);
return responseJson;
case 400:
throw BadRequestException(response.body.toString());
case 401:
case 403:
throw UnauthorisedException(response.body.toString());
case 500:
default:
throw FetchDataException(
'Error occured while Communication with Server with StatusCode : ${response.statusCode}');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment