Skip to content

Commit

Permalink
Merge pull request #10 from argentlabs/fix/errors
Browse files Browse the repository at this point in the history
fix: hybrid session error
  • Loading branch information
bluecco committed Apr 24, 2024
2 parents 19d3360 + dc1282d commit fef4a34
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ The following snippet show how to create and use an hybrid session account
```typescript
import {
SignSessionError,
SessionParams,
createSessionAccount
} from "@argent/x-sessions"
Expand Down Expand Up @@ -100,15 +101,20 @@ const sessionAccount = await createSessionAccount({
})

// this transaction should get executed without the user having to approve again
const tx = sessionAccount.execute({
// lets assume this is a erc20 contract
contractAddress: "0x...",
selector: "transfer",
calldata: [
"0x..."
// ...
]
})

try {
const tx = sessionAccount.execute({
// lets assume this is a erc20 contract
contractAddress: "0x...",
selector: "transfer",
calldata: [
"0x..."
// ...
]
})
} catch (e) {
console.error((e as SignSessionError).cause, e.message)
}
```

Use this account to send transactions.
Expand Down
10 changes: 10 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export class SignSessionError extends Error {
constructor(
message: string,
public cause: any,
) {
super(message)

Error.captureStackTrace(this, this.constructor)
}
}
8 changes: 7 additions & 1 deletion src/hybridSessionBackendService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
BackendSignSessionBody,
BackendSignatureResponse,
} from "./hybridSessionTypes"
import { SignSessionError } from "./errors"

export class ArgentBackendService {
constructor(
Expand Down Expand Up @@ -129,8 +130,13 @@ export class ArgentBackendService {
},
body: JSON.stringify(body),
})
const json = await response.json()

if (!response.ok) {
const error: { status: string } = await response.json()
throw new SignSessionError("Sign session error", error.status)
}

const json = await response.json()
return json.signature
}

Expand Down
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export type {
CreateSessionParams,
SessionParams,
} from "./hybridSessionTypes"

export { type SignSessionError } from "./errors"

0 comments on commit fef4a34

Please sign in to comment.