Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ts: add error log parsing to ts client #1640

Merged
merged 23 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ incremented for features.
* lang: Handle arrays with const as size in instruction data ([#1623](https://github.com/project-serum/anchor/issues/1623).
* spl: Add support for revoke instruction ([#1493](https://github.com/project-serum/anchor/pull/1493)).
* ts: Add provider parameter to `Spl.token` factory method ([#1597](https://github.com/project-serum/anchor/pull/1597)).
* ts: Add `AnchorError` with program stack and also a program stack for non-`AnchorError` errors ([#1640](https://github.com/project-serum/anchor/pull/1640)). `AnchorError` is not returned for `processed` tx that have `skipPreflight` set to `true` (it falls back to `ProgramError` or the raw solana library error).

### Fixes

Expand Down
41 changes: 27 additions & 14 deletions tests/bpf-upgradeable-state/tests/bpf-upgradable-state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as anchor from "@project-serum/anchor";
import { Program } from "@project-serum/anchor";
import { AnchorError, Program } from "@project-serum/anchor";
import { findProgramAddressSync } from "@project-serum/anchor/dist/cjs/utils/pubkey";
import { PublicKey } from "@solana/web3.js";
import assert from "assert";
Expand Down Expand Up @@ -78,9 +78,11 @@ describe("bpf_upgradeable_state", () => {
signers: [settings, authority],
});
assert.ok(false);
} catch (err) {
assert.equal(err.code, 2003);
assert.equal(err.msg, "A raw constraint was violated");
} catch (_err) {
assert.ok(_err instanceof AnchorError);
const err: AnchorError = _err;
assert.equal(err.error.errorCode.number, 2003);
assert.equal(err.error.errorMessage, "A raw constraint was violated");
}
});

Expand All @@ -98,9 +100,14 @@ describe("bpf_upgradeable_state", () => {
signers: [settings],
});
assert.ok(false);
} catch (err) {
assert.equal(err.code, 3013);
assert.equal(err.msg, "The given account is not a program data account");
} catch (_err) {
assert.ok(_err instanceof AnchorError);
const err: AnchorError = _err;
assert.equal(err.error.errorCode.number, 3013);
assert.equal(
err.error.errorMessage,
"The given account is not a program data account"
);
}
});

Expand All @@ -118,10 +125,12 @@ describe("bpf_upgradeable_state", () => {
signers: [settings],
});
assert.ok(false);
} catch (err) {
assert.equal(err.code, 3007);
} catch (_err) {
assert.ok(_err instanceof AnchorError);
const err: AnchorError = _err;
assert.equal(err.error.errorCode.number, 3007);
assert.equal(
err.msg,
err.error.errorMessage,
"The given account is owned by a different program than expected"
);
}
Expand Down Expand Up @@ -149,8 +158,10 @@ describe("bpf_upgradeable_state", () => {
signers: [settings],
});
assert.ok(false);
} catch (err) {
assert.equal(err.code, 6000);
} catch (_err) {
assert.ok(_err instanceof AnchorError);
const err: AnchorError = _err;
assert.equal(err.error.errorCode.number, 6000);
}
});

Expand All @@ -176,8 +187,10 @@ describe("bpf_upgradeable_state", () => {
signers: [settings],
});
assert.ok(false);
} catch (err) {
assert.equal(err.code, 2003);
} catch (_err) {
assert.ok(_err instanceof AnchorError);
const err: AnchorError = _err;
assert.equal(err.error.errorCode.number, 2003);
}
});
});
2 changes: 1 addition & 1 deletion tests/declare-id/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ wallet = "~/.config/solana/id.json"
declare_id = "FJcF5c8HncdfAgjPjTH49GAEypkJCG2ZADh2xhduNi5B"

[scripts]
test = "yarn run mocha -t 1000000 tests/"
test = "yarn run ts-mocha -t 1000000 tests/*.ts"
17 changes: 0 additions & 17 deletions tests/declare-id/tests/declare-id.js

This file was deleted.

22 changes: 22 additions & 0 deletions tests/declare-id/tests/declare-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as anchor from "@project-serum/anchor";
import { AnchorError, Program } from "@project-serum/anchor";
import splToken from "@solana/spl-token";
import { DeclareId } from "../target/types/declare_id";

import { assert } from "chai";

describe("declare_id", () => {
anchor.setProvider(anchor.Provider.local());
const program = anchor.workspace.DeclareId as Program<DeclareId>;

it("throws error!", async () => {
try {
await program.rpc.initialize();
assert.ok(false);
} catch (_err) {
assert.ok(_err instanceof AnchorError);
const err: AnchorError = _err;
assert.equal(err.error.errorCode.number, 4100);
}
});
});
10 changes: 10 additions & 0 deletions tests/declare-id/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"types": ["mocha", "chai", "node"],
"typeRoots": ["./node_modules/@types"],
"lib": ["es2015"],
"module": "commonjs",
"target": "es6",
"esModuleInterop": true
}
}
2 changes: 1 addition & 1 deletion tests/errors/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ wallet = "~/.config/solana/id.json"
errors = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"

[scripts]
test = "yarn run mocha -t 1000000 tests/"
test = "yarn run ts-mocha -t 1000000 tests/*.ts"

[features]
seeds = false
Loading