Files
nitro/packages/react-native-nitro-modules/cpp/utils/AssertPromiseState.hpp
T
Marc RousavyandGitHub b069fb69a8 fix: Fix Promise rejections not having a message by using std::exception_ptr to avoid C++ object slicing (#369)
* fix: Use `std::exception_ptr` to preserve message

* fix: Catch exception

* fix: Fix `JSIConverter`

* Update AssertPromiseState.hpp

* Ok kinda fix?

* ugh

* fix again

* Update JSIConverter+Promise.hpp

* chore: Rename `_result` to `_state`

* feat: Allow comparing message

* fix: Fix `RuntimeError.description`

* feat: Test all of those error messages as well
2024-11-22 21:14:00 +01:00

35 lines
888 B
C++

//
// AssertPromiseState.hpp
// NitroModules
//
// Created by Marc Rousavy on 20.11.24.
//
#pragma once
namespace margelo::nitro {
template <typename TResult>
class Promise;
} // namespace margelo::nitro
#include "Promise.hpp"
#include "TypeInfo.hpp"
#include <exception>
#include <string>
namespace margelo::nitro {
enum PromiseTask { WANTS_TO_RESOLVE, WANTS_TO_REJECT };
template <typename TResult>
void assertPromiseState(Promise<TResult>& promise, PromiseTask task) {
if (!promise.isPending()) [[unlikely]] {
std::string taskString = task == WANTS_TO_RESOLVE ? "resolve" : "reject";
std::string state = promise.isResolved() ? "resolved" : "rejected";
throw std::runtime_error("Cannot " + taskString + " Promise<" + TypeInfo::getFriendlyTypename<TResult>() + "> - it is already " +
state + "!");
}
}
} // namespace margelo::nitro