Skip to content

Commit

Permalink
better error dialog in info page
Browse files Browse the repository at this point in the history
  • Loading branch information
Predidit committed Dec 15, 2024
1 parent 49942f7 commit db482fc
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 deletions.
1 change: 1 addition & 0 deletions lib/bean/card/bangumi_history_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class _BangumiHistoryCardVState extends State<BangumiHistoryCardV> {
} catch (e) {
KazumiLogger().log(Level.warning, e.toString());
KazumiDialog.dismiss();
KazumiDialog.showToast(message: '网络资源获取失败 ${e.toString()}');
}
},
child: Padding(
Expand Down
26 changes: 14 additions & 12 deletions lib/bean/dialog/dialog_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,20 @@ class KazumiDialog {
}) {
final scaffoldContext = context ?? _observer.currentContext;
if (scaffoldContext != null) {
ScaffoldMessenger.of(scaffoldContext).showSnackBar(
SnackBar(
content: Text(message),
duration: duration,
action: showUndoButton
? SnackBarAction(
label: 'Dismiss',
onPressed: () {},
)
: null,
),
);
ScaffoldMessenger.of(scaffoldContext)
..removeCurrentSnackBar()
..showSnackBar(
SnackBar(
content: Text(message),
duration: duration,
action: showUndoButton
? SnackBarAction(
label: 'Dismiss',
onPressed: () {},
)
: null,
),
);
} else {
debugPrint(
'Kazumi Dialog Error: No Scaffold context available to show Toast');
Expand Down
13 changes: 10 additions & 3 deletions lib/plugins/plugins.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ class Plugin {
return data;
}

Future<PluginSearchResponse> queryBangumi(String keyword, {bool shouldRethrow = false}) async {
Future<PluginSearchResponse> queryBangumi(String keyword,
{bool shouldRethrow = false}) async {
String queryURL = searchURL.replaceAll('@keyword', keyword);
dynamic resp;
List<SearchItem> searchItems = [];
Expand All @@ -133,14 +134,17 @@ class Plugin {
};
resp = await Request().post(postUri.toString(),
options: Options(headers: httpHeaders),
extra: {'customError': '规则检索错误: $name 响应不符合预期'},
data: queryParams,
shouldRethrow: shouldRethrow);
} else {
var httpHeaders = {
'referer': '$baseUrl/',
};
resp = await Request().get(queryURL,
options: Options(headers: httpHeaders), shouldRethrow: shouldRethrow);
options: Options(headers: httpHeaders),
shouldRethrow: shouldRethrow,
extra: {'customError': '规则检索错误: $name 响应不符合预期'});
}

var htmlString = resp.data.toString();
Expand Down Expand Up @@ -194,7 +198,10 @@ class Plugin {
chapterNameList.add(itemName.replaceAll(RegExp(r'\s+'), ''));
});
if (chapterUrlList.isNotEmpty && chapterNameList.isNotEmpty) {
Road road = Road(name: '播放列表$count', data: chapterUrlList, identifier: chapterNameList);
Road road = Road(
name: '播放列表$count',
data: chapterUrlList,
identifier: chapterNameList);
roadList.add(road);
count++;
}
Expand Down
13 changes: 9 additions & 4 deletions lib/request/interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ class ApiInterceptor extends Interceptor {
void onError(DioException err, ErrorInterceptorHandler handler) async {
String url = err.requestOptions.uri.toString();
if (!url.contains('heartBeat')) {
KazumiDialog.showToast(
message: await dioError(err),
);
if (err.requestOptions.extra['customError'] == null) {
KazumiDialog.showToast(
message: await dioError(err),
);
} else {
KazumiDialog.showToast(
message: err.requestOptions.extra['customError'],
);
}
}
super.onError(err, handler);
// super.onError(err, handler);
}

static Future<String> dioError(DioException error) async {
Expand Down
4 changes: 3 additions & 1 deletion lib/request/request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,11 @@ class Request {
if (extra['ua'] != null) {
options.headers = {'user-agent': headerUa(type: extra['ua'])};
}
if (extra['customError'] != null) {
options.extra = {'customError': extra['customError']};
}
}
options.responseType = resType;

try {
response = await dio.get(
url,
Expand Down

0 comments on commit db482fc

Please sign in to comment.