-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from aserto-dev/custom_errors
add custom errors
- Loading branch information
Showing
7 changed files
with
218 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
"codecov", | ||
"connectrpc", | ||
"displaystatemap", | ||
"instanceof", | ||
"keyof", | ||
"morty", | ||
"njwt", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -471,6 +471,21 @@ Get an object instance with the type `type-name` and the id `object-id`. For exa | |
|
||
```typescript | ||
const user = await directoryClient.object({ objectType: 'user', objectId: '[email protected]' }); | ||
|
||
// Handle a specific Directory Error | ||
import { NotFoundError } from "@aserto/aserto-node" | ||
|
||
try { | ||
directoryClient.object({ | ||
objectType: "user", | ||
objectId: "[email protected]", | ||
}); | ||
} catch (error) { | ||
if (error instanceof NotFoundError) { | ||
// handle the case where the object was not found | ||
} | ||
throw error; | ||
} | ||
``` | ||
|
||
#### 'relation' function | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,11 +24,18 @@ import { | |
SetRelationResponse, | ||
} from "@aserto/node-directory/src/gen/cjs/aserto/directory/writer/v3/writer_pb"; | ||
import { Struct } from "@bufbuild/protobuf"; | ||
import { ConnectError } from "@connectrpc/connect"; | ||
import { Code, ConnectError } from "@connectrpc/connect"; | ||
import { createAsyncIterable } from "@connectrpc/connect/protocol"; | ||
import * as connectNode from "@connectrpc/connect-node"; | ||
|
||
import { DirectoryServiceV3, DirectoryV3 } from "../../../lib/index"; | ||
import { | ||
DirectoryServiceV3, | ||
DirectoryV3, | ||
EtagMismatchError, | ||
InvalidArgumentError, | ||
NotFoundError, | ||
UnauthenticatedError, | ||
} from "../../../lib/index"; | ||
jest.mock("fs"); | ||
|
||
describe("DirectoryV3", () => { | ||
|
@@ -234,7 +241,7 @@ describe("DirectoryV3", () => { | |
it("handles ConnectError", async () => { | ||
const mockCheckPermission = jest | ||
.spyOn(directory.ReaderClient, "checkPermission") | ||
.mockRejectedValue(new ConnectError("connect error", 5)); | ||
.mockRejectedValue(new ConnectError("connect error", Code.Canceled)); | ||
|
||
const params = { | ||
subjectId: "[email protected]", | ||
|
@@ -243,8 +250,42 @@ describe("DirectoryV3", () => { | |
objectType: "group", | ||
objectId: "admin", | ||
}; | ||
|
||
// error class | ||
await expect(directory.checkPermission(params)).rejects.toThrow( | ||
ConnectError | ||
); | ||
|
||
// error message | ||
await expect(directory.checkPermission(params)).rejects.toThrow( | ||
'"checkPermission" failed with code: 5, message: [not_found] connect error' | ||
'"checkPermission" failed with code: 1, message: [canceled] connect error' | ||
); | ||
|
||
mockCheckPermission.mockReset(); | ||
}); | ||
|
||
it("handles Unauthenticated Error", async () => { | ||
const mockCheckPermission = jest | ||
.spyOn(directory.ReaderClient, "checkPermission") | ||
.mockRejectedValue( | ||
new ConnectError("Invalid credentials", Code.Unauthenticated) | ||
); | ||
|
||
const params = { | ||
subjectId: "[email protected]", | ||
subjectType: "user", | ||
permission: "read", | ||
objectType: "group", | ||
objectId: "admin", | ||
}; | ||
|
||
// error class | ||
await expect(directory.checkPermission(params)).rejects.toThrow( | ||
UnauthenticatedError | ||
); | ||
// error message | ||
await expect(directory.checkPermission(params)).rejects.toThrow( | ||
"Authentication failed: [unauthenticated] Invalid credentials" | ||
); | ||
|
||
mockCheckPermission.mockReset(); | ||
|
@@ -372,6 +413,23 @@ describe("DirectoryV3", () => { | |
|
||
mockGetObject.mockReset(); | ||
}); | ||
|
||
it("handles NotFound Error", async () => { | ||
const mockGetObject = jest | ||
.spyOn(directory.ReaderClient, "getObject") | ||
.mockRejectedValue(new ConnectError("Not found", Code.NotFound)); | ||
|
||
const params = { objectId: "123", objectType: "user" }; | ||
|
||
// error class | ||
await expect(directory.object(params)).rejects.toThrow(NotFoundError); | ||
// error message | ||
await expect(directory.object(params)).rejects.toThrow( | ||
"object not found" | ||
); | ||
|
||
mockGetObject.mockReset(); | ||
}); | ||
}); | ||
|
||
describe("objects", () => { | ||
|
@@ -454,6 +512,48 @@ describe("DirectoryV3", () => { | |
|
||
mockSetObject.mockReset(); | ||
}); | ||
|
||
it("handles InvalidArgument Error", async () => { | ||
const mockSetObject = jest | ||
.spyOn(directory.WriterClient, "setObject") | ||
.mockRejectedValue( | ||
new ConnectError("Invalid argument", Code.InvalidArgument) | ||
); | ||
|
||
const params = {}; | ||
|
||
// error class | ||
await expect(directory.setObject(params)).rejects.toThrow( | ||
InvalidArgumentError | ||
); | ||
// error message | ||
await expect(directory.setObject(params)).rejects.toThrow( | ||
"setObject: [invalid_argument] Invalid argument" | ||
); | ||
|
||
mockSetObject.mockReset(); | ||
}); | ||
|
||
it("handles EtagMissmatch Error", async () => { | ||
const mockSetObject = jest | ||
.spyOn(directory.WriterClient, "setObject") | ||
.mockRejectedValue( | ||
new ConnectError("Invalid argument", Code.FailedPrecondition) | ||
); | ||
|
||
const params = {}; | ||
|
||
// error class | ||
await expect(directory.setObject(params)).rejects.toThrow( | ||
EtagMismatchError | ||
); | ||
// error message | ||
await expect(directory.setObject(params)).rejects.toThrow( | ||
"invalid etag in setObject request" | ||
); | ||
|
||
mockSetObject.mockReset(); | ||
}); | ||
}); | ||
|
||
describe("objectMany", () => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Custom error class for directory service operations. | ||
* Extends the built-in Error class. | ||
* | ||
* @class DirectoryService | ||
* @extends Error | ||
*/ | ||
class DirectoryServiceError extends Error {} | ||
/** | ||
* Object or Relation is not found. | ||
* Extends the DirectoryServiceError class. | ||
* | ||
* @class NotFoundError | ||
* @extends DirectoryServiceError | ||
*/ | ||
export class NotFoundError extends DirectoryServiceError {} | ||
/** | ||
* "Invalid Argument" error. | ||
* Extends the DirectoryServiceError class. | ||
* | ||
* @class InvalidArgumentError | ||
* @extends DirectoryServiceError | ||
*/ | ||
export class InvalidArgumentError extends DirectoryServiceError {} | ||
/** | ||
* "Etag Mismatch" error. | ||
* Extends the DirectoryServiceError class. | ||
* | ||
* @class EtagMismatchError | ||
* @extends DirectoryServiceError | ||
*/ | ||
export class EtagMismatchError extends DirectoryServiceError {} | ||
/** | ||
* "Unauthenticated" error. | ||
* Extends the DirectoryServiceError class. | ||
* | ||
* @class UnauthenticatedError | ||
* @extends DirectoryServiceError | ||
*/ | ||
export class UnauthenticatedError extends DirectoryServiceError {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,3 +58,5 @@ export { | |
SubIdentityMapper, | ||
DirectoryConfig as ServiceConfig, | ||
}; | ||
|
||
export * from "./directory/errors"; |