Skip to content

Commit e0ea30c

Browse files
authored
Render error codes on Rust renderer (#31)
* Render error codes * Add changeset * Fix lint * Remove console log
1 parent 3645d92 commit e0ea30c

File tree

4 files changed

+57
-20
lines changed

4 files changed

+57
-20
lines changed

.changeset/yellow-deers-pay.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@kinobi-so/renderers-rust": patch
3+
---
4+
5+
Render error codes on Rust renderer

packages/renderers-rust/e2e/system/src/generated/errors/system.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,33 @@ use thiserror::Error;
1010

1111
#[derive(Clone, Debug, Eq, Error, FromPrimitive, PartialEq)]
1212
pub enum SystemError {
13-
/// 0 (0x0) - an account with the same address already exists
13+
/// 0 - an account with the same address already exists
1414
#[error("an account with the same address already exists")]
15-
AccountAlreadyInUse,
16-
/// 1 (0x1) - account does not have enough SOL to perform the operation
15+
AccountAlreadyInUse = 0x0,
16+
/// 1 - account does not have enough SOL to perform the operation
1717
#[error("account does not have enough SOL to perform the operation")]
18-
ResultWithNegativeLamports,
19-
/// 2 (0x2) - cannot assign account to this program id
18+
ResultWithNegativeLamports = 0x1,
19+
/// 2 - cannot assign account to this program id
2020
#[error("cannot assign account to this program id")]
21-
InvalidProgramId,
22-
/// 3 (0x3) - cannot allocate account data of this length
21+
InvalidProgramId = 0x2,
22+
/// 3 - cannot allocate account data of this length
2323
#[error("cannot allocate account data of this length")]
24-
InvalidAccountDataLength,
25-
/// 4 (0x4) - length of requested seed is too long
24+
InvalidAccountDataLength = 0x3,
25+
/// 4 - length of requested seed is too long
2626
#[error("length of requested seed is too long")]
27-
MaxSeedLengthExceeded,
28-
/// 5 (0x5) - provided address does not match addressed derived from seed
27+
MaxSeedLengthExceeded = 0x4,
28+
/// 5 - provided address does not match addressed derived from seed
2929
#[error("provided address does not match addressed derived from seed")]
30-
AddressWithSeedMismatch,
31-
/// 6 (0x6) - advancing stored nonce requires a populated RecentBlockhashes sysvar
30+
AddressWithSeedMismatch = 0x5,
31+
/// 6 - advancing stored nonce requires a populated RecentBlockhashes sysvar
3232
#[error("advancing stored nonce requires a populated RecentBlockhashes sysvar")]
33-
NonceNoRecentBlockhashes,
34-
/// 7 (0x7) - stored nonce is still in recent_blockhashes
33+
NonceNoRecentBlockhashes = 0x6,
34+
/// 7 - stored nonce is still in recent_blockhashes
3535
#[error("stored nonce is still in recent_blockhashes")]
36-
NonceBlockhashNotExpired,
37-
/// 8 (0x8) - specified nonce does not match stored nonce
36+
NonceBlockhashNotExpired = 0x7,
37+
/// 8 - specified nonce does not match stored nonce
3838
#[error("specified nonce does not match stored nonce")]
39-
NonceUnexpectedBlockhashValue,
39+
NonceUnexpectedBlockhashValue = 0x8,
4040
}
4141

4242
impl solana_program::program_error::PrintProgramError for SystemError {

packages/renderers-rust/public/templates/errorsPage.njk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use thiserror::Error;
88
#[derive(Clone, Debug, Eq, Error, FromPrimitive, PartialEq)]
99
pub enum {{ program.name | pascalCase }}Error {
1010
{% for error in errors | sort(false, false, 'code') %}
11-
/// {{ error.code }} (0x{{ error.code.toString(16) | upper }}) - {{ error.message }}
11+
/// {{ error.code }} - {{ error.message }}
1212
#[error("{{ error.message }}")]
13-
{{ error.name | pascalCase }},
13+
{{ error.name | pascalCase }} = 0x{{ error.code.toString(16) | upper }},
1414
{% endfor %}
1515
}
1616

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { errorNode, programNode } from '@kinobi-so/nodes';
2+
import { visit } from '@kinobi-so/visitors-core';
3+
import { test } from 'vitest';
4+
5+
import { getRenderMapVisitor } from '../src';
6+
import { codeContains } from './_setup';
7+
8+
test('it renders codes for errors', () => {
9+
// Given the following program with 2 errors.
10+
const node = programNode({
11+
errors: [
12+
errorNode({
13+
code: 6000,
14+
message: 'Invalid instruction',
15+
name: 'InvalidInstruction',
16+
}),
17+
errorNode({
18+
code: 7000,
19+
message: 'Invalid program',
20+
name: 'InvalidProgram',
21+
}),
22+
],
23+
name: 'splToken',
24+
publicKey: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
25+
});
26+
27+
// When we render it.
28+
const renderMap = visit(node, getRenderMapVisitor());
29+
30+
// Then we expect the following errors with codes.
31+
codeContains(renderMap.get('errors/spl_token.rs'), [`InvalidInstruction = 0x1770,`, `InvalidProgram = 0x1B58,`]);
32+
});

0 commit comments

Comments
 (0)