createUser method
Example of POST request with error handling
Implementation
Future<Map<String, dynamic>> createUser(Map<String, dynamic> userData) async {
try {
final environment = BaseEnvironment(
baseUrl: 'api.example.com',
platformPath: 'api/v1',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
);
final endpoint = GtdEndpoint(env: environment, path: 'users');
final networkRequest = GTDNetworkRequest(
type: GtdMethod.post,
enpoint: endpoint,
data: userData,
);
networkService.request = networkRequest;
final response = await networkService.execute();
return response.data;
} on DioException catch (e) {
final gtdError = GtdError.fromDioError(e);
// Example of custom error handling based on status code
if (gtdError.statusCode == 422) {
// Handle validation errors - parse the validation errors
// but we're not using them in the custom error anymore since we removed data field
_extractValidationErrors(e.response?.data); // Just call the function without storing the result
throw GtdError.custom(
'Validation failed',
statusCode: gtdError.statusCode,
errorCode: 'VALIDATION_ERROR',
);
}
_logError(gtdError);
throw gtdError;
} catch (e, stackTrace) {
final gtdError = GtdError.fromException(e, stackTrace);
_logError(gtdError);
throw gtdError;
}
}