Skip to content

Commit

Permalink
refactor: rework getRestrictedErrorDescription function (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
halildurmus committed Jul 7, 2023
1 parent 6e0c814 commit 0c2a7a9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ Future<void> _completeAsyncDelegate<T extends IAsyncInfo, C>(T asyncDelegate,
if (status == AsyncStatus.completed) {
onCompleted();
} else if (status == AsyncStatus.error) {
completer.completeError(WindowsException(asyncDelegate.errorCode,
message: getRestrictedErrorDescription()));
final errorCode = asyncDelegate.errorCode;
completer.completeError(WindowsException(errorCode,
message: getRestrictedErrorDescription(errorCode)));
} else if (status == AsyncStatus.canceled) {
completer.completeError('The async operation canceled!');
}
Expand Down
18 changes: 13 additions & 5 deletions packages/windows_foundation/lib/src/internal/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ import '../winrt_struct.dart';
///
/// Returns `null` if no restricted error description was found.
/// @nodoc
String? getRestrictedErrorDescription() {
String? getRestrictedErrorDescription(int hresult) {
final ppRestrictedErrorInfo = calloc<COMObject>();

try {
var hr = GetRestrictedErrorInfo(ppRestrictedErrorInfo.cast());
if (FAILED(hr)) throw WindowsException(hr);
if (ppRestrictedErrorInfo.isNull) {
free(ppRestrictedErrorInfo);
return null;
}

if (ppRestrictedErrorInfo.isNull) return null;
final restrictedErrorInfo = IRestrictedErrorInfo(ppRestrictedErrorInfo);

return using((arena) {
Expand All @@ -35,8 +38,13 @@ String? getRestrictedErrorDescription() {
description, error, restrictedDescription, capabilitySid);
if (FAILED(hr)) throw WindowsException(hr);

final value = restrictedDescription.value.toDartString();
return value.isEmpty ? null : value;
// If the error is not the one we're looking for, return null.
if (error.value != hresult) return null;

final message = restrictedDescription.value.toDartString();
if (message.isNotEmpty) return message;
// If the restricted description is empty, return the generic description.
return description.value.toDartString();
});
} catch (_) {
free(ppRestrictedErrorInfo);
Expand All @@ -48,7 +56,7 @@ String? getRestrictedErrorDescription() {
/// description of the last error that occurred on the current logical thread.
/// @nodoc
void throwWindowsException(int hr) =>
throw WindowsException(hr, message: getRestrictedErrorDescription());
throw WindowsException(hr, message: getRestrictedErrorDescription(hr));

/// Determines whether [S] is the same type as [T].
///
Expand Down

0 comments on commit 0c2a7a9

Please sign in to comment.