Line data Source code
1 : import 'dart:convert'; 2 : 3 : /// Exception related to api calls 4 : class StreamApiException implements Exception { 5 : //TODO: test this 6 : /// Creates a new ApiError instance using the response body and status code 7 2 : StreamApiException(this.body, this.status) : jsonData = _decode(body) { 8 3 : if (jsonData != null && jsonData!.containsKey('code')) { 9 3 : _code = jsonData!['code']; 10 : } 11 : } 12 : 13 : /// Raw body of the response 14 : final String? body; 15 : 16 : /// Json parsed body 17 : final Map<String, dynamic>? jsonData; 18 : 19 : /// Http status code of the response 20 : final int? status; 21 : 22 : /// Stream specific error code 23 2 : int? get code => _code; 24 : int? _code; 25 : 26 1 : static Map<String, dynamic>? _decode(String? body) { 27 : try { 28 : if (body == null) { 29 : return null; 30 : } 31 1 : return json.decode(body); 32 0 : } on FormatException { 33 : return null; 34 : } 35 : } 36 : 37 0 : @override 38 : bool operator ==(Object other) => 39 : identical(this, other) || 40 0 : other is StreamApiException && 41 0 : runtimeType == other.runtimeType && 42 0 : body == other.body && 43 0 : jsonData == other.jsonData && 44 0 : status == other.status && 45 0 : _code == other._code; 46 : 47 0 : @override 48 : int get hashCode => 49 0 : body.hashCode ^ jsonData.hashCode ^ status.hashCode ^ _code.hashCode; 50 : 51 0 : @override 52 : String toString() => 53 : // ignore: lines_longer_than_80_chars 54 0 : 'StreamApiException{body: $body, jsonData: $jsonData, status: $status, code: $_code}'; 55 : }