diff --git a/lib/src/model/matrix_exception.dart b/lib/src/model/matrix_exception.dart index e04aee1e..28e14445 100644 --- a/lib/src/model/matrix_exception.dart +++ b/lib/src/model/matrix_exception.dart @@ -24,6 +24,7 @@ import 'dart:convert'; import 'package:http/http.dart' as http; +import 'package:matrix_api_lite/matrix_api_lite.dart'; enum MatrixError { M_UNKNOWN, @@ -65,12 +66,12 @@ class MatrixException implements Exception { /// The unique identifier for this error. String get errcode => - raw['errcode'] as String? ?? + raw.tryGet('errcode') ?? (requireAdditionalAuthentication ? 'M_FORBIDDEN' : 'M_UNKNOWN'); /// A human readable error description. String get errorMessage => - raw['error'] as String? ?? + raw.tryGet('error') ?? (requireAdditionalAuthentication ? 'Require additional authentication' : 'Unknown error'); @@ -91,11 +92,11 @@ class MatrixException implements Exception { (e) => e.toString() == 'MatrixError.${(raw["errcode"] ?? "")}', orElse: () => MatrixError.M_UNKNOWN); - int? get retryAfterMs => raw['retry_after_ms'] as int?; + int? get retryAfterMs => raw.tryGet('retry_after_ms'); /// This is a session identifier that the client must pass back to the homeserver, if one is provided, /// in subsequent attempts to authenticate in the same API call. - String? get session => raw['session'] as String?; + String? get session => raw.tryGet('session'); /// Returns true if the server requires additional authentication. bool get requireAdditionalAuthentication => response != null @@ -105,26 +106,24 @@ class MatrixException implements Exception { /// For each endpoint, a server offers one or more 'flows' that the client can use /// to authenticate itself. Each flow comprises a series of stages. If this request /// doesn't need additional authentication, then this is null. - List? get authenticationFlows { - final flows = raw['flows']; - return (flows is List>>) - ? flows - .map((flow) => flow['stages']) - .whereType>() - .map((stages) => AuthenticationFlow(List.from(stages))) - .toList() - : null; - } + List? get authenticationFlows => raw + .tryGet>('flows') + ?.whereType>() + .map((flow) => flow['stages']) + .whereType>() + .map((stages) => + AuthenticationFlow(List.from(stages.whereType()))) + .toList(); /// This section contains any information that the client will need to know in order to use a given type /// of authentication. For each authentication type presented, that type may be present as a key in this /// dictionary. For example, the public part of an OAuth client ID could be given here. Map? get authenticationParams => - raw['params'] as Map?; + raw.tryGetMap('params'); /// Returns the list of already completed authentication flows from previous requests. List get completedAuthenticationFlows => - List.from(raw['completed'] as List? ?? []); + raw.tryGetList('completed') ?? []; } /// For each endpoint, a server offers one or more 'flows' that the client can use