fetchUserProfile method
Example of GET request with proper error handling
Implementation
Future<Map<String, dynamic>> fetchUserProfile(String userId) async {
try {
// Setup environment and endpoint
final environment = BaseEnvironment(
baseUrl: 'api.example.com',
platformPath: 'api/v1',
headers: {'Accept': 'application/json'},
);
final endpoint = GtdEndpoint(env: environment, path: 'users/$userId');
// Configure request
final networkRequest = GTDNetworkRequest(
type: GtdMethod.get,
enpoint: endpoint,
);
networkService.request = networkRequest;
// Execute request
final response = await networkService.execute();
return response.data;
} on DioException catch (e) {
// Transform DioException to our standardized GtdError
final gtdError = GtdError.fromDioError(e);
// Log the detailed error for developers
_logError(gtdError);
// You can handle specific error codes here
if (gtdError.statusCode == 401) {
// Handle authentication error
// e.g., refresh token or redirect to login
}
// Rethrow as our standardized error
throw gtdError;
} catch (e, stackTrace) {
// Handle other exceptions
final gtdError = GtdError.fromException(e, stackTrace);
_logError(gtdError);
throw gtdError;
}
}