Is there a way to use throw errors (throw Error.reject
) in none async function?
Or is there a way to throw an exception in a query function in a none async context?
In one of my canister I have got the following query function:
public shared query({ caller }) func getStorage() : async ?BucketId {
let result: {#bucketId: ?BucketId; #error: Text;} = storagesStore.getBucket(caller);
switch (result) {
case (#error error) {
throw Error.reject(error);
};
case (#bucketId bucketId) {
switch (bucketId) {
case (?bucketId) {
return ?bucketId;
};
case null {
return null;
};
};
};
};
};
I’ve made my storage generic and therefore would like to refactor it to use it multiple times:
public shared query({ caller }) func getStorage(): async (?BucketId) {
return getBucket<StorageBucket>(caller, storagesStore);
};
public shared query({ caller }) func getWhateverBucket(): async (?BucketId) {
return getBucket<AnotherTypeOfBucket>(caller, myOtherStore);
};
private func getBucket<T>(caller: Principal, store: BucketsStore.BucketsStore<T>): (?BucketId) {
let result: {#bucketId: ?BucketId; #error: Text;} = store.getBucket(caller);
switch (result) {
case (#error error) {
throw Error.reject(error);
};
case (#bucketId bucketId) {
switch (bucketId) {
case (?bucketId) {
return ?bucketId;
};
case null {
return null;
};
};
};
};
};
However, I cannot do so as throw Error.reject
needs an async context (misplaced throw
).