diff --git a/lib/core/exceptions/http_exception.dart b/lib/core/exceptions/http_exception.dart index 948b8975..73cbc24c 100644 --- a/lib/core/exceptions/http_exception.dart +++ b/lib/core/exceptions/http_exception.dart @@ -1,6 +1,6 @@ /* * This file is part of wger Workout Manager . - * Copyright (c) 2020 - 2025 wger Team + * Copyright (c) 2020 - 2026 wger Team * * wger Workout Manager is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -43,7 +43,8 @@ class WgerHttpException implements Exception { final dynamic responseBody = response.body; final contentType = response.headers[HttpHeaders.contentTypeHeader]; - if (contentType != null && contentType.contains('text/html')) { + if ((contentType != null && contentType.contains('text/html')) || + responseBody.toString().contains('. - * Copyright (c) 2020 - 2025 wger Team + * Copyright (c) 2020 - 2026 wger Team * * wger Workout Manager is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -51,8 +51,6 @@ void showHttpExceptionErrorDialog(WgerHttpException exception, {BuildContext? co return; } - final theme = Theme.of(dialogContext); - showDialog( context: dialogContext, builder: (ctx) => AlertDialog( diff --git a/lib/providers/auth.dart b/lib/providers/auth.dart index edb73a8d..3f2e26fa 100644 --- a/lib/providers/auth.dart +++ b/lib/providers/auth.dart @@ -1,6 +1,6 @@ /* * This file is part of wger Workout Manager . - * Copyright (c) 2020 - 2025 wger Team + * Copyright (c) 2020 - 2026 wger Team * * wger Workout Manager is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -58,7 +58,7 @@ class AuthProvider with ChangeNotifier { static const SERVER_VERSION_URL = 'version'; static const REGISTRATION_URL = 'register'; static const LOGIN_URL = 'login'; - static const TEST_URL = 'userprofile'; + static const USERPROFILE_URL = 'userprofile'; late http.Client client; @@ -150,7 +150,7 @@ class AuthProvider with ChangeNotifier { // Login using the API token if (apiToken != null && apiToken.isNotEmpty) { final response = await client.get( - makeUri(serverUrl, TEST_URL), + makeUri(serverUrl, USERPROFILE_URL), headers: { HttpHeaders.contentTypeHeader: 'application/json; charset=UTF-8', HttpHeaders.userAgentHeader: getAppNameHeader(), diff --git a/test/core/http_exception_test.dart b/test/core/http_exception_test.dart index 26b7a754..30ae455c 100644 --- a/test/core/http_exception_test.dart +++ b/test/core/http_exception_test.dart @@ -1,6 +1,6 @@ /* * This file is part of wger Workout Manager . - * Copyright (c) 2020 - 2025 wger Team + * Copyright (c) 2020 - 2026 wger Team * * wger Workout Manager is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -25,20 +25,24 @@ import 'package:wger/core/exceptions/http_exception.dart'; void main() { group('WgerHttpException', () { test('parses valid JSON response', () { + // Arrange final resp = http.Response( '{"foo":"bar"}', 400, headers: {HttpHeaders.contentTypeHeader: 'application/json'}, ); + // Act final ex = WgerHttpException(resp); + // Assert expect(ex.type, ErrorType.json); expect(ex.errors['foo'], 'bar'); expect(ex.toString(), contains('WgerHttpException')); }); test('falls back on malformed JSON', () { + // Arrange const body = '{"foo":'; final resp = http.Response( body, @@ -46,13 +50,16 @@ void main() { headers: {HttpHeaders.contentTypeHeader: 'application/json'}, ); + // Act final ex = WgerHttpException(resp); + // Assert expect(ex.type, ErrorType.json); expect(ex.errors['unknown_error'], body); }); - test('detects HTML response', () { + test('detects HTML response from headers', () { + // Arrange const body = 'Error'; final resp = http.Response( body, @@ -60,16 +67,39 @@ void main() { headers: {HttpHeaders.contentTypeHeader: 'text/html; charset=utf-8'}, ); + // Act final ex = WgerHttpException(resp); + // Assert + expect(ex.type, ErrorType.html); + expect(ex.htmlError, body); + }); + + test('detects HTML response from content', () { + // Arrange + const body = 'Error'; + final resp = http.Response( + body, + 500, + headers: {HttpHeaders.contentTypeHeader: 'text/foo; charset=utf-8'}, + ); + + // Act + final ex = WgerHttpException(resp); + + // Assert expect(ex.type, ErrorType.html); expect(ex.htmlError, body); }); test('fromMap sets errors and type', () { + // Arrange final map = {'field': 'value'}; + + // Act final ex = WgerHttpException.fromMap(map); + // Assert expect(ex.type, ErrorType.json); expect(ex.errors, map); });