LockallyException.fromResponse constructor

LockallyException.fromResponse({
  1. required int status,
  2. Object? body,
  3. String? requestId,
})

Parse a problem+json body (falls back to a generic message).

Implementation

factory LockallyException.fromResponse({
  required int status,
  Object? body,
  String? requestId,
}) {
  Map<String, dynamic>? obj;
  if (body is Map<String, dynamic>) {
    obj = body;
  } else if (body is String && body.isNotEmpty) {
    try {
      final decoded = jsonDecode(body);
      if (decoded is Map<String, dynamic>) obj = decoded;
    } catch (_) {/* non-JSON body */}
  }
  if (obj == null) {
    return LockallyException(status: status, message: 'HTTP $status', requestId: requestId);
  }
  final message = (obj['detail'] ?? obj['title'] ?? 'HTTP $status').toString();
  final code = (obj['code'] ?? obj['type'])?.toString();
  return LockallyException(
    status: status,
    message: message,
    code: code,
    requestId: requestId ?? obj['request_id']?.toString(),
  );
}