Skip to content

Commit

Permalink
[graphql] Add support for clever error resolution in graphql (#17338)
Browse files Browse the repository at this point in the history
## Description 

Adds support for clever errors to GraphQL. So now, if you have code like
the following:
```move
module p::m {
   #[error]
    const ENotFound: vector<u8> = b"Element was unable to be found in the vector"

    public fun abort() {
        assert!(false, ENotFound); // Can also be an `abort ENotFound` 
    }
}
```

This will result in a humand-readable output from graphql:
```
Error in 1st command
'ENotFound' from module '0x8fd7251015bfd1dc4283bb3d38dc95a2769f75d621c871a81bb9c191a2860aaf::m' in function 'abort' at source line 6
The element was unable to be found in the vector
```

## Test plan 

Added tests for all supported constant types, along with tests to make
sure that we properly handle package upgrades and resolving to the
correct version of a package when resolving clever errors.

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [X] GraphQL: Adds support for more understandable and ergonomic Move
error codes in Move 2024.
- [ ] CLI: 
- [ ] Rust SDK:
  • Loading branch information
tzakian committed May 13, 2024
1 parent 4074d9e commit 74750a6
Show file tree
Hide file tree
Showing 10 changed files with 805 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

248 changes: 248 additions & 0 deletions crates/sui-graphql-e2e-tests/tests/errors/clever_errors.exp

Large diffs are not rendered by default.

233 changes: 233 additions & 0 deletions crates/sui-graphql-e2e-tests/tests/errors/clever_errors.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

//# init --protocol-version 39 --addresses P0=0x0 P1=0x0 --accounts A --simulator

//# publish --upgradeable --sender A
module P0::m {
#[error]
const ImAU8: u8 = 0;

#[error]
const ImAU16: u16 = 1;

#[error]
const ImAU32: u32 = 2;

#[error]
const ImAU64: u64 = 3;

#[error]
const ImAU128: u128 = 4;

#[error]
const ImAU256: u256 = 5;

#[error]
const ImABool: bool = true;

#[error]
const ImAnAddress: address = @6;

#[error]
const ImAString: vector<u8> = b"This is a string";

#[error]
const ImNotAString: vector<u64> = vector[1,2,3,4,5];

public fun callU8() {
abort ImAU8
}

public fun callU16() {
abort ImAU16
}

public fun callU32() {
abort ImAU32
}

public fun callU64() {
abort ImAU64
}

public fun callU128() {
abort ImAU128
}

public fun callU256() {
abort ImAU256
}

public fun callAddress() {
abort ImAnAddress
}

public fun callString() {
abort ImAString
}

public fun callU64vec() {
abort ImNotAString
}

public fun normalAbort() {
abort 0
}

public fun assertLineNo() {
assert!(false);
}
}

//# run P0::m::callU8

//# run P0::m::callU16

//# run P0::m::callU32

//# run P0::m::callU64

//# run P0::m::callU128

//# run P0::m::callU256

//# run P0::m::callAddress

//# run P0::m::callString

//# run P0::m::callU64vec

//# run P0::m::normalAbort

//# run P0::m::assertLineNo

//# create-checkpoint

//# run-graphql
{
transactionBlocks(last: 11) {
nodes {
effects {
status
errors
}
}
}
}

//# upgrade --package P0 --upgrade-capability 1,1 --sender A
// Upgrade the module with new error values but using the same constant names
// (etc) to make sure we properly resolve the module location for clever
// errors.
module P0::m {
#[error]
const ImAU8: u8 = 7;

#[error]
const ImAU16: u16 = 8;

#[error]
const ImAU32: u32 = 9;

#[error]
const ImAU64: u64 = 10;

#[error]
const ImAU128: u128 = 11;

#[error]
const ImAU256: u256 = 12;

#[error]
const ImABool: bool = false;

#[error]
const ImAnAddress: address = @13;

#[error]
const ImAString: vector<u8> = b"This is a string in v2";

#[error]
const ImNotAString: vector<u64> = vector[1,2,3,4,5,6];

public fun callU8() {
abort ImAU8
}

public fun callU16() {
abort ImAU16
}

public fun callU32() {
abort ImAU32
}

public fun callU64() {
abort ImAU64
}

public fun callU128() {
abort ImAU128
}

public fun callU256() {
abort ImAU256
}

public fun callAddress() {
abort ImAnAddress
}

public fun callString() {
abort ImAString
}

public fun callU64vec() {
abort ImNotAString
}

public fun normalAbort() {
abort 0
}

public fun assertLineNo() {
assert!(false);
}
}

//# run P0::m::callU8

//# run P0::m::callU16

//# run P0::m::callU32

//# run P0::m::callU64

//# run P0::m::callU128

//# run P0::m::callU256

//# run P0::m::callAddress

//# run P0::m::callString

//# run P0::m::callU64vec

//# run P0::m::normalAbort

//# run P0::m::assertLineNo

//# create-checkpoint

//# run-graphql
{
transactionBlocks(last: 9) {
nodes {
effects {
status
errors
}
}
}
}
4 changes: 2 additions & 2 deletions crates/sui-graphql-e2e-tests/tests/transactions/errors.exp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Response: {
{
"effects": {
"status": "FAILURE",
"errors": "Move Runtime Abort. Location: 83f3be7571dccac8f0fd562cf4aa66707bafe3996c49b6f659be49a9841653fe::m::boom (function index 1) at offset 1, Abort Code: 42 in 1st command."
"errors": "Error in 1st command, from '0x83f3be7571dccac8f0fd562cf4aa66707bafe3996c49b6f659be49a9841653fe::m::boom' (instruction 1), abort code: 42"
}
}
]
Expand All @@ -43,7 +43,7 @@ Response: {
{
"effects": {
"status": "FAILURE",
"errors": "Move Runtime Abort. Location: 83f3be7571dccac8f0fd562cf4aa66707bafe3996c49b6f659be49a9841653fe::m::boom (function index 1) at offset 1, Abort Code: 42 in 3rd command."
"errors": "Error in 3rd command, from '0x83f3be7571dccac8f0fd562cf4aa66707bafe3996c49b6f659be49a9841653fe::m::boom' (instruction 1), abort code: 42"
}
}
]
Expand Down
2 changes: 2 additions & 0 deletions crates/sui-graphql-rpc/schema/current_progress_schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3709,6 +3709,8 @@ type TransactionBlockEffects {
lamportVersion: Int!
"""
The reason for a transaction failure, if it did fail.
If the error is a Move abort, the error message will be resolved to a human-readable form if
possible, otherwise it will fall back to displaying the abort code and location.
"""
errors: String
"""
Expand Down
Loading

0 comments on commit 74750a6

Please sign in to comment.