simplified code across the app and consolidated duplicated helpers
This commit is contained in:
@@ -2,8 +2,8 @@ import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../../../errors/app_exception.dart';
|
||||
import '../../../errors/ticker_content_unavailable_exception.dart';
|
||||
import '../../errors/marianumconnect_error.dart';
|
||||
import '../../marianumconnect_query.dart';
|
||||
import 'get_ticker_page_response.dart';
|
||||
|
||||
@@ -14,26 +14,20 @@ import 'get_ticker_page_response.dart';
|
||||
/// 404 `{ "error": "CONTENT_UNAVAILABLE", "webUrl": ... }`; that case is mapped
|
||||
/// to a dedicated [TickerContentUnavailableException] carrying the browser
|
||||
/// fallback URL, so the detail screen can offer "open in browser" instead of a
|
||||
/// generic error. The bespoke 404 handling is why this keeps its own try/catch
|
||||
/// instead of the base [guard].
|
||||
/// generic error.
|
||||
class GetTickerPage extends MarianumConnectQuery {
|
||||
final String slug;
|
||||
|
||||
GetTickerPage(this.slug, {super.dio});
|
||||
|
||||
Future<TickerPageResponse> run() async {
|
||||
try {
|
||||
final response = await dio.get<Map<String, dynamic>>(
|
||||
endpoint('ticker/pages/${Uri.encodeComponent(slug)}'),
|
||||
);
|
||||
return TickerPageResponse.fromJson(response.data!);
|
||||
} on DioException catch (e) {
|
||||
throw _mapError(e);
|
||||
}
|
||||
}
|
||||
Future<TickerPageResponse> run() => getObject(
|
||||
'ticker/pages/${Uri.encodeComponent(slug)}',
|
||||
TickerPageResponse.fromJson,
|
||||
);
|
||||
|
||||
Object _mapError(DioException e) {
|
||||
final response = e.response;
|
||||
@override
|
||||
AppException mapError(DioException error) {
|
||||
final response = error.response;
|
||||
if (response?.statusCode == 404) {
|
||||
final body = _asMap(response?.data);
|
||||
if (body != null && body['error'] == 'CONTENT_UNAVAILABLE') {
|
||||
@@ -44,7 +38,7 @@ class GetTickerPage extends MarianumConnectQuery {
|
||||
);
|
||||
}
|
||||
}
|
||||
return mapMarianumConnectError(e);
|
||||
return super.mapError(error);
|
||||
}
|
||||
|
||||
Map<String, dynamic>? _asMap(dynamic data) {
|
||||
|
||||
Reference in New Issue
Block a user