diff --git a/geolocator_windows/windows/geolocator_enums.h b/geolocator_windows/windows/geolocator_enums.h index d45bcc90..dfb9e146 100644 --- a/geolocator_windows/windows/geolocator_enums.h +++ b/geolocator_windows/windows/geolocator_enums.h @@ -1,7 +1,9 @@ namespace geolocator_plugin { enum ErrorCode { - PermissionDefinitionsNotFound + PermissionDefinitionsNotFound, + OperationCanceled, + UnknownError }; enum LocationPermission { @@ -31,4 +33,4 @@ enum ServiceStatus { Enabled }; -} // namespace geolocator_plugin \ No newline at end of file +} // namespace geolocator_plugin diff --git a/geolocator_windows/windows/geolocator_plugin.cpp b/geolocator_windows/windows/geolocator_plugin.cpp index 8e78ecb3..7ac507af 100644 --- a/geolocator_windows/windows/geolocator_plugin.cpp +++ b/geolocator_windows/windows/geolocator_plugin.cpp @@ -25,8 +25,12 @@ std::string ErrorCodeToString(ErrorCode errorCode) { switch (errorCode) { case ErrorCode::PermissionDefinitionsNotFound: return "PERMISSION_DEFINITIONS_NOT_FOUND"; + case ErrorCode::OperationCanceled: + return "OPERATION_CANCELED"; + case ErrorCode::UnknownError: + return "UNKNOWN_ERROR"; default: - return ""; + throw std::logic_error("unexcepted value" + static_cast(errorCode)); } } @@ -146,8 +150,13 @@ void GeolocatorPlugin::OnCheckPermission(std::unique_ptr> result) RequestAccessAsync(std::move(result)); } +bool isLocationStatusValid (const PositionStatus& status) { + return status != PositionStatus::Disabled + && status != PositionStatus::NotAvailable; +} + void GeolocatorPlugin::OnIsLocationServiceEnabled(std::unique_ptr> result) { - result->Success(EncodableValue(geolocator.LocationStatus() != PositionStatus::NotAvailable)); + result->Success(EncodableValue(isLocationStatusValid(geolocator.LocationStatus()))); } void GeolocatorPlugin::OnRequestPermission(std::unique_ptr> result) { @@ -156,8 +165,18 @@ void GeolocatorPlugin::OnRequestPermission(std::unique_ptr> resul winrt::fire_and_forget GeolocatorPlugin::OnGetLastKnownPosition(const MethodCall<>& method_call, std::unique_ptr> result) { - auto location = co_await geolocator.GetGeopositionAsync(std::chrono::hours(1), std::chrono::milliseconds::zero()); - result->Success(LocationToEncodableMap(location)); + try { + auto location = co_await geolocator.GetGeopositionAsync(std::chrono::hours(1), std::chrono::milliseconds::zero()); + result->Success(LocationToEncodableMap(location)); + } + catch (hresult_canceled const& error) { + result->Error(ErrorCodeToString(ErrorCode::OperationCanceled), + to_string(error.message())); + } + catch (hresult_error const& error) { + result->Error(ErrorCodeToString(ErrorCode::UnknownError), + to_string(error.message())); + } } void GeolocatorPlugin::GetLocationAccuracy(std::unique_ptr> result) { @@ -169,9 +188,18 @@ void GeolocatorPlugin::GetLocationAccuracy(std::unique_ptr> resul winrt::fire_and_forget GeolocatorPlugin::OnGetCurrentPosition(const MethodCall<>& method_call, std::unique_ptr> result) { - - auto location = co_await geolocator.GetGeopositionAsync(); - result->Success(LocationToEncodableMap(location)); + try { + auto location = co_await geolocator.GetGeopositionAsync(); + result->Success(LocationToEncodableMap(location)); + } + catch (hresult_canceled const& error) { + result->Error(ErrorCodeToString(ErrorCode::OperationCanceled), + to_string(error.message())); + } + catch (hresult_error const& error) { + result->Error(ErrorCodeToString(ErrorCode::UnknownError), + to_string(error.message())); + } } std::unique_ptr> GeolocatorPlugin::OnListen(