Skip to content

Commit

Permalink
added error handling and error state UI
Browse files Browse the repository at this point in the history
  • Loading branch information
sunder-kirei committed Feb 24, 2023
1 parent 13caf71 commit fe26838
Show file tree
Hide file tree
Showing 7 changed files with 464 additions and 268 deletions.
17 changes: 17 additions & 0 deletions lib/helpers/http_exception.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class ApiException implements Exception {
final String message;
final String error;
ApiException({
required this.message,
required this.error,
});

@override
String toString() {
return message;
}

String get getBaseError {
return error;
}
}
57 changes: 46 additions & 11 deletions lib/helpers/http_helper.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// ignore_for_file: constant_identifier_names

import 'dart:convert';
import 'package:anime_api/helpers/http_exception.dart';
import 'package:http/http.dart' as http;

enum GetLanding {
Expand All @@ -12,30 +13,50 @@ enum GetLanding {
class HttpHelper {
static const baseUrl = "anime-api-vnkr.onrender.com";

//broken
static Future<Map<String, dynamic>> searchApi({
required String query,
}) async {
final url = Uri.https(baseUrl, "/search/$query");
final response = await http.get(url);
return json.decode(response.body) as Map<String, dynamic>;
try {
final response = await http.get(url);
return json.decode(response.body) as Map<String, dynamic>;
} catch (err) {
throw ApiException(
error: err.toString(),
message: "Failed to search.",
);
}
}

static Future<Map<String, dynamic>> getInfo({
required int malID,
}) async {
final url = Uri.https(baseUrl, "/info/$malID");
final response = await http.get(url);
return json.decode(response.body) as Map<String, dynamic>;
try {
final response = await http.get(url);
return json.decode(response.body) as Map<String, dynamic>;
} catch (err) {
throw ApiException(
error: err.toString(),
message: "Failed to load info.",
);
}
}

static Future<List<dynamic>> getVideoSources({
required String episodeID,
required String animeId,
}) async {
final url = Uri.https(baseUrl, "/watch/$animeId/$episodeID");
final response = await http.get(url);
return json.decode(response.body) as List<dynamic>;
try {
final response = await http.get(url);
return json.decode(response.body) as List<dynamic>;
} catch (err) {
throw ApiException(
error: err.toString(),
message: "Failed to load streaming info.",
);
}
}

static Future<Map<String, dynamic>> getEpisodeList({
Expand All @@ -44,15 +65,29 @@ class HttpHelper {
String? season = "unknown",
}) async {
final url = Uri.https(baseUrl, "$title/$releasedYear/$season");
final response = await http.get(url);
return json.decode(response.body) as Map<String, dynamic>;
try {
final response = await http.get(url);
return json.decode(response.body) as Map<String, dynamic>;
} catch (err) {
throw ApiException(
error: err.toString(),
message: "Failed to load episode info.",
);
}
}

static Future<Map<String, dynamic>> getLanding({
required GetLanding landing,
}) async {
final url = Uri.https(baseUrl, landing.name);
final response = await http.get(url);
return json.decode(response.body);
try {
final response = await http.get(url);
return json.decode(response.body);
} catch (err) {
throw ApiException(
error: err.toString(),
message: "Looks like there was a network failure.",
);
}
}
}
Loading

0 comments on commit fe26838

Please sign in to comment.