Skip to content

Commit

Permalink
fix: catch exceptions when checking for updates
Browse files Browse the repository at this point in the history
Resolves #195
  • Loading branch information
Merrit committed Oct 27, 2023
1 parent f1eaaf8 commit c4a76f6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
20 changes: 16 additions & 4 deletions lib/app_version/app_version.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:http/http.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:pub_semver/pub_semver.dart' as semver;

Expand Down Expand Up @@ -34,13 +35,23 @@ class AppVersion {
/// Gets the latest version from the GitHub tag.
Future<String> latest() async {
if (_latest != '') return _latest;

final uri = Uri.parse(
'https://api.github.com/repos/merrit/nyrna/releases',
);
final response = await http.get(
uri,
headers: {'Accept': 'application/vnd.github.v3+json'},
);

final Response response;

try {
response = await http.get(
uri,
headers: {'Accept': 'application/vnd.github.v3+json'},
);
} on Exception catch (e) {
log.w('Issue getting latest version info from GitHub: $e\n');
return '';
}

if (response.statusCode == 200) {
final json = jsonDecode(response.body) as List;
final data = List<Map>.from(json);
Expand All @@ -51,6 +62,7 @@ class AppVersion {
log.w('Issue getting latest version info from GitHub, '
'status code: ${response.statusCode}\n');
}

return _latest;
}

Expand Down
16 changes: 12 additions & 4 deletions lib/updates/update_service.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:http/http.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:pub_semver/pub_semver.dart';

Expand Down Expand Up @@ -46,10 +47,17 @@ class UpdateService {
'https://api.github.com/repos/merrit/nyrna/releases',
);

final response = await http.get(
uri,
headers: {'Accept': 'application/vnd.github.v3+json'},
);
final Response response;

try {
response = await http.get(
uri,
headers: {'Accept': 'application/vnd.github.v3+json'},
);
} on Exception catch (e) {
log.w('Issue getting latest version info from GitHub: $e\n');
return null;
}

if (response.statusCode == 200) {
final json = jsonDecode(response.body) as List;
Expand Down

0 comments on commit c4a76f6

Please sign in to comment.