Created
June 29, 2021 11:01
-
-
Save zamahaka/45b47208b7228c725161f061a8cd778f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'package:dio/dio.dart'; | |
import 'package:flutter/foundation.dart'; | |
import 'package:json_annotation/json_annotation.dart'; | |
import 'package:mymilkman/crashlytics.dart'; | |
int maybeResponseCode(dynamic error, {bool gatherValidation = true}) { | |
if (error is DioError && error.type == DioErrorType.RESPONSE) { | |
return error.response.statusCode; | |
} | |
return null; | |
} | |
String maybeResponseError(dynamic error, {bool gatherValidation = true}) { | |
String errorMessage; | |
if (error is DioError && error.type == DioErrorType.RESPONSE) { | |
error = error.response.data; | |
} | |
if (error is MissingRequiredKeysException && kDebugMode) { | |
errorMessage = 'MissingRequiredKeysException ( ${error.missingKeys} )'; | |
print(errorMessage); | |
} else { | |
try { | |
if (error is String) error = jsonDecode(error); | |
if (error is Map<String, dynamic> && | |
error['ValidationErrors'] != null && | |
error['ValidationErrors'] is Map<String, dynamic> && | |
gatherValidation) { | |
final validationMap = error['ValidationErrors'] as Map<String, dynamic>; | |
errorMessage = validationMap.values.join(', '); | |
} else if (error is Map<String, dynamic> && | |
error.containsKey('Message')) { | |
errorMessage = error['Message']; | |
} else { | |
errorMessage = null; | |
} | |
} catch (e, s) { | |
reportError(e, s, reason: 'while parsing network error'); | |
} | |
} | |
return errorMessage?.toString(); | |
} | |
Map<String, String> maybeValidationError(dynamic error) { | |
if (error is DioError && error.type == DioErrorType.RESPONSE) { | |
error = error.response.data; | |
} | |
if (error is! MissingRequiredKeysException) { | |
try { | |
if (error is Map<String, dynamic> && | |
error['ValidationErrors'] != null && | |
error['ValidationErrors'] is Map<String, dynamic>) { | |
final validationMap = error['ValidationErrors'] as Map<String, dynamic>; | |
return validationMap | |
.map((key, value) => MapEntry(key, value.toString())); | |
} | |
} catch (e, s) { | |
reportError(e, s, reason: 'while parsing network validation error'); | |
} | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment