/* * This file is part of wger Workout Manager . * 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 * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ import 'dart:convert'; import 'dart:io'; import 'package:http/http.dart'; enum ErrorType { json, html, text, } const HTML_ERROR_KEY = 'html_error'; class WgerHttpException implements Exception { Map errors = {}; /// The exception type. While the majority will be json, it is possible that /// the server will return HTML, e.g. if there has been an internal server error /// or similar. late ErrorType type; /// Custom http exception WgerHttpException(Response response) { type = ErrorType.json; final dynamic responseBody = response.body; final contentType = response.headers[HttpHeaders.contentTypeHeader]; if ((contentType != null && contentType.contains('text/html')) || responseBody.toString().contains('(); } else if (type == ErrorType.html) { errors = {HTML_ERROR_KEY: responseBody.toString()}; } else { errors = {'text_error': responseBody.toString()}; } } catch (e) { errors = {'unknown_error': responseBody}; } } } WgerHttpException.fromMap(Map map) : type = ErrorType.json { errors = map; } String get htmlError { if (type != ErrorType.html) { return ''; } return errors[HTML_ERROR_KEY] ?? ''; } @override String toString() { return 'WgerHttpException ($type): $errors'; } }