Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Handle mosque not found error in API and update mosque manager #1162

Merged
merged 3 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/src/domain/model/failure/mosque/mosque_failure.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
abstract class MosqueFailure implements Exception {
final String errorMessage;
final String errorCode;

const MosqueFailure({
required this.errorMessage,
required this.errorCode,
});
}

class MosqueNotFoundFailure extends MosqueFailure {
const MosqueNotFoundFailure()
: super(
errorMessage: 'Mosque not found',
errorCode: 'MOSQUE_IS_NOT_FOUND',
);
}
22 changes: 16 additions & 6 deletions lib/src/helpers/Api.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:math';
import 'dart:developer';
import 'dart:math' hide log;

import 'package:device_info_plus/device_info_plus.dart';
import 'package:dio/dio.dart';
Expand All @@ -19,6 +20,7 @@ import 'package:package_info_plus/package_info_plus.dart';
import 'package:unique_identifier/unique_identifier.dart';
import 'package:xml_parser/xml_parser.dart';

import '../domain/model/failure/mosque/mosque_failure.dart';
import '../models/hijri_data_config_model.dart';
import '../models/mosque.dart';
import '../models/weather.dart';
Expand Down Expand Up @@ -98,11 +100,19 @@ class Api {
}

static Future<Mosque> getMosque(String id) async {
final response = await dio.get(
'/3.0/mosque/$id/info',
);

return Mosque.fromMap(response.data);
try {
final response = await dio.get(
'/3.0/mosque/$id/info',
);
return Mosque.fromMap(response.data);
} on DioException catch (e) {
// error 404
if (e.response != null && e.response?.statusCode == 404) {
log('Mosque not found');
throw MosqueNotFoundFailure();
}
rethrow;
}
}

/// re check the mosque config if there are any updated data
Expand Down
7 changes: 7 additions & 0 deletions lib/src/services/mosque_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:http/http.dart' as http;
import 'package:mawaqit/main.dart';
import 'package:mawaqit/src/domain/model/failure/mosque/mosque_failure.dart';
import 'package:mawaqit/src/helpers/Api.dart';
import 'package:mawaqit/src/helpers/PerformanceHelper.dart';
import 'package:mawaqit/src/helpers/SharedPref.dart';
Expand Down Expand Up @@ -132,6 +133,12 @@ class MosqueManager extends ChangeNotifier with WeatherMixin, AudioMixin, Mosque

/// if getting item returns an error
onItemError(e, stack) {
if (e is MosqueFailure) {
mosque = null;
notifyListeners();
return;
}

logger.e(e, stackTrace: stack);

mosque = null;
Expand Down
Loading