Skip to content

Commit 15fd9e7

Browse files
committed
better error handling for auth failures
1 parent ff91f4f commit 15fd9e7

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

src/data/collections/causal/CausalArray.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { ClassRegistry } from 'data/model/literals';
1515
import { MutableContentEvents } from 'data/model/mutable/MutableObject';
1616
import { MultiMap } from 'util/multimap';
1717
import { InvalidateAfterOp } from 'data/model/causal';
18-
import { BaseCausalCollection, CausalCollection, CausalCollectionConfig, CausalCollectionOp } from './CausalCollection';
18+
import { AuthError, BaseCausalCollection, CausalCollection, CausalCollectionConfig, CausalCollectionOp } from './CausalCollection';
1919
import { Identity } from 'data/identity';
2020

2121
// A mutable list with a
@@ -264,7 +264,7 @@ class CausalArray<T> extends BaseCausalCollection<T> implements CausalCollection
264264
this.setCurrentPrevOpsTo(insertOp);
265265

266266
if (!(await auth.attempt(insertOp))) {
267-
throw new Error('Cannot authorize insertion operation on CausalArray ' + this.hash() + ', author is: ' + author?.hash());
267+
throw new AuthError('Cannot authorize insertion operation on CausalArray ' + this.hash() + ', author is: ' + author?.hash());
268268
}
269269

270270
await this.applyNewOp(insertOp);
@@ -398,7 +398,7 @@ class CausalArray<T> extends BaseCausalCollection<T> implements CausalCollection
398398
this.setCurrentPrevOpsTo(deleteOp);
399399

400400
if (!(await auth.attempt(deleteOp))) {
401-
throw new Error('Cannot authorize delete operation on CausalArray ' + this.hash() + ', author is: ' + author?.hash());
401+
throw new AuthError('Cannot authorize delete operation on CausalArray ' + this.hash() + ', author is: ' + author?.hash());
402402
}
403403

404404
deleteOps.push(deleteOp);

src/data/collections/causal/CausalCollection.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { Hash} from '../../model/hashing'
44
import { Authorization, Authorizer } from 'data/model';
55
import { BaseCollection, CollectionConfig } from '../mutable/Collection';
66

7+
class AuthError extends Error {
8+
9+
};
10+
711
interface CausalCollection<T> {
812
has(elmt: T): boolean;
913
hasByHash(hash: Hash): boolean;
@@ -92,5 +96,5 @@ abstract class CausalCollectionOp extends MutationOp {
9296
}
9397
}
9498

95-
export { CausalCollection, BaseCausalCollection, CausalCollectionOp };
99+
export { CausalCollection, BaseCausalCollection, CausalCollectionOp, AuthError };
96100
export type { CausalCollectionConfig };

src/data/collections/causal/CausalSet.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Authorization, Verification } from '../../model/causal/Authorization';
88
import { HashedSet } from '../../model/immutable/HashedSet';
99
import { MutableSetEvents } from '../../collections/mutable/MutableSet';
1010
import { MutableContentEvents } from '../../model/mutable/MutableObject';
11-
import { BaseCausalCollection, CausalCollection, CausalCollectionConfig, CausalCollectionOp } from './CausalCollection';
11+
import { AuthError, BaseCausalCollection, CausalCollection, CausalCollectionConfig, CausalCollectionOp } from './CausalCollection';
1212
import { ClassRegistry } from 'data/model/literals';
1313

1414
/*
@@ -234,7 +234,7 @@ class CausalSet<T> extends BaseCausalCollection<T> implements CausalCollection<T
234234
this.setCurrentPrevOpsTo(addOp);
235235

236236
if (!(await auth.attempt(addOp))) {
237-
throw new Error('Cannot authorize addition operation on CausalSet ' + this.hash() + ', author is: ' + author?.hash());
237+
throw new AuthError('Cannot authorize addition operation on CausalSet ' + this.hash() + ', author is: ' + author?.hash());;
238238
}
239239

240240
return this.applyNewOp(addOp).then(() => true);
@@ -268,7 +268,7 @@ class CausalSet<T> extends BaseCausalCollection<T> implements CausalCollection<T
268268
this.setCurrentPrevOpsTo(deleteOp);
269269

270270
if (!(await auth.attempt(deleteOp))) {
271-
throw new Error('Cannot authorize delete operation on CausalSet ' + this.hash() + ', author is: ' + author?.hash());
271+
throw new AuthError('Cannot authorize delete operation on CausalSet ' + this.hash() + ', author is: ' + author?.hash());
272272
}
273273

274274
deleteOps.push(deleteOp);

test/data/types/FeatureSet.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CausalSet, SingleAuthorCausalSet, CausalSetAddOp, CausalSetDeleteOp } from 'data/collections';
2+
import { AuthError } from 'data/collections/causal/CausalCollection';
23
import { Identity } from 'data/identity';
34
import { Hash, HashedObject, MutationOp } from 'data/model';
45
import { Authorizer, Authorization } from 'data/model';
@@ -40,7 +41,12 @@ class FeatureSet extends CausalSet<Feature> {
4041
try {
4142
return await super.add(feature, author, auth);
4243
} catch (e) {
43-
return false;
44+
if (e instanceof AuthError) {
45+
return false;
46+
} else {
47+
throw e;
48+
}
49+
4450
}
4551
}
4652

@@ -55,7 +61,12 @@ class FeatureSet extends CausalSet<Feature> {
5561
try {
5662
return await super.delete(feature, author, auth);
5763
} catch (e) {
58-
return false;
64+
if (e instanceof AuthError) {
65+
return false;
66+
} else {
67+
throw e;
68+
}
69+
5970
}
6071
}
6172

@@ -64,9 +75,13 @@ class FeatureSet extends CausalSet<Feature> {
6475
const auth = this.createAuthorizerFor(author);
6576

6677
try {
67-
return super.deleteByHash(hash, author, auth);
78+
return await super.deleteByHash(hash, author, auth);
6879
} catch (e) {
69-
return false;
80+
if (e instanceof AuthError) {
81+
return false;
82+
} else {
83+
throw e;
84+
}
7085
}
7186

7287
}

0 commit comments

Comments
 (0)