Skip to content

Commit

Permalink
feat: Handle mosque not found error in API and update mosque manager (#…
Browse files Browse the repository at this point in the history
…1162)

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

- Added `MosqueFailure` and `MosqueNotFoundFailure` classes to handle specific mosque-related errors.
- Updated `Api.dart` to catch Dio 404 errors and throw `MosqueNotFoundFailure`.
- Modified `mosque_manager.dart` to handle `MosqueFailure` errors specifically and update state accordingly.

* reformat
  • Loading branch information
YassinNouh21 authored Jul 11, 2024
1 parent b080b88 commit 86835e8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
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 @@ -6,6 +6,7 @@ import 'package:flutter/services.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/const/constants.dart';
import 'package:mawaqit/src/helpers/Api.dart';
import 'package:mawaqit/src/helpers/PerformanceHelper.dart';
Expand Down Expand Up @@ -169,6 +170,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

0 comments on commit 86835e8

Please sign in to comment.