Skip to content

Commit

Permalink
near.panic() deleted (#171)
Browse files Browse the repository at this point in the history
near.panic() deleted
  • Loading branch information
volovyks authored Aug 15, 2022
1 parent 6767e39 commit 1db2e87
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 79 deletions.
2 changes: 1 addition & 1 deletion examples/src/cross-contract-call.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class OnCall extends NearContract {
_set_person_on_call_private({ accountId }) {
near.log(`_set_person_on_call_private called, accountId ${accountId}`)
if (near.currentAccountId() !== near.predecessorAccountId()) {
near.panic('Function can be used as a callback only')
throw Error('Function can be used as a callback only')
}
const status = JSON.parse(near.promiseResult(0))
near.log(`${accountId} status is ${status}`)
Expand Down
2 changes: 1 addition & 1 deletion examples/src/non-fungible-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class NftContract extends NearContract {
_nftResolveTransfer({ sender_id, receiver_id, token_id }) {
near.log(`_nftResolveTransfer called, receiver_id ${receiver_id}, token_id ${token_id}`)
if (near.currentAccountId() == !near.predecessorAccountId()) {
near.panic('Function can be used as a callback only')
throw Error('Function can be used as a callback only')
}
const isTokenTransfered = JSON.parse(near.promiseResult(0))
near.log(`${token_id} ${isTokenTransfered ? 'was transfered' : 'was NOT transfered'}`)
Expand Down
12 changes: 6 additions & 6 deletions jsvm/tests/src/bytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,27 @@ export function storage_read_utf8_sequence_bytes_key_arbitrary_bytes_value() {
}

export function panic_test() {
near.panic()
throw Error()
}

export function panic_ascii_test() {
near.panic('abc')
throw Error('abc')
}

export function panic_js_number() {
near.panic(356)
throw Error(356)
}

export function panic_js_undefined() {
near.panic(undefined)
throw Error(undefined)
}

export function panic_js_null() {
near.panic(null)
throw Error(null)
}

export function panic_utf8_test() {
near.panic('水')
throw Error('水')
}

export function panicUtf8_valid_utf8_sequence() {
Expand Down
1 change: 0 additions & 1 deletion lib/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export declare function keccak256(value: Bytes): Bytes;
export declare function keccak512(value: Bytes): Bytes;
export declare function ripemd160(value: Bytes): Bytes;
export declare function ecrecover(hash: Bytes, sig: Bytes, v: number, malleabilityFlag: number): Bytes | null;
export declare function panic(msg?: string): never;
export declare function panicUtf8(msg: string): never;
export declare function logUtf8(msg: string): void;
export declare function logUtf16(msg: string): void;
Expand Down
11 changes: 2 additions & 9 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,7 @@ export function ecrecover(hash, sig, v, malleabilityFlag) {
}
return env.read_register(0);
}
export function panic(msg) {
if (msg !== undefined) {
env.panic(msg);
}
else {
env.panic();
}
}
// NOTE: "env.panic(msg)" is not exported, use "throw Error(msg)" instead
export function panicUtf8(msg) {
env.panic_utf8(msg);
}
Expand Down Expand Up @@ -268,7 +261,7 @@ export function promiseResult(resultIdx) {
return status;
}
else {
panic(`Unexpected return code: ${status}`);
throw Error(`Unexpected return code: ${status}`);
}
}
export function promiseReturn(promiseIdx) {
Expand Down
11 changes: 2 additions & 9 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const U64_MAX = 2n ** 64n - 1n;
const EVICTED_REGISTER = U64_MAX - 1n;

interface Env {
panic: (msg?: string) => never;
panic_utf8: (msg: string) => never;
[x: string]: any;
}
Expand Down Expand Up @@ -100,13 +99,7 @@ export function ecrecover(
return env.read_register(0);
}

export function panic(msg?: string): never {
if (msg !== undefined) {
env.panic(msg);
} else {
env.panic();
}
}
// NOTE: "env.panic(msg)" is not exported, use "throw Error(msg)" instead

export function panicUtf8(msg: string): never {
env.panic_utf8(msg);
Expand Down Expand Up @@ -424,7 +417,7 @@ export function promiseResult(
) {
return status;
} else {
panic(`Unexpected return code: ${status}`);
throw Error(`Unexpected return code: ${status}`);
}
}

Expand Down
42 changes: 21 additions & 21 deletions tests/__tests__/bytes.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,45 +168,45 @@ test('Storage read bytes tests', async t => {
test('panic tests', async t => {
const { ali, bytesContract } = t.context.accounts;
let r = await ali.callRaw(bytesContract, 'panic_test', '');
t.deepEqual(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError,
'Smart contract panicked: explicit guest panic'
t.assert(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError
.match(/Smart contract panicked:*/)
);

r = await ali.callRaw(bytesContract, 'panic_ascii_test', '');
t.deepEqual(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError,
'Smart contract panicked: abc'
t.assert(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError
.match(/Smart contract panicked: abc*/)
);

r = await ali.callRaw(bytesContract, 'panic_js_number', '');
t.deepEqual(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError,
'Smart contract panicked: 356'
t.assert(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError
.match(/Smart contract panicked: 356*/)
);

r = await ali.callRaw(bytesContract, 'panic_js_undefined', '');
t.deepEqual(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError,
'Smart contract panicked: explicit guest panic'
t.assert(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError
.match(/Smart contract panicked:*/)
);

r = await ali.callRaw(bytesContract, 'panic_js_null', '');
t.deepEqual(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError,
'Smart contract panicked: null'
t.assert(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError
.match(/Smart contract panicked: null*/)
);

r = await ali.callRaw(bytesContract, 'panic_utf8_test', '');
t.deepEqual(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError,
'Smart contract panicked: 水'
t.assert(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError
.match(/Smart contract panicked: 水*/)
);

r = await ali.callRaw(bytesContract, 'panicUtf8_valid_utf8_sequence', '');
t.deepEqual(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError,
'Smart contract panicked: 水'
t.assert(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError
.match(/Smart contract panicked: 水*/)
);

r = await ali.callRaw(bytesContract, 'panicUtf8_invalid_utf8_sequence', '');
Expand Down
37 changes: 18 additions & 19 deletions tests/__tests__/test_log_panic_api.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,39 +86,38 @@ test('Log invalid utf-16 sequence panic', async t => {
test('panic tests', async t => {
const { ali, testContract } = t.context.accounts;
let r = await ali.callRaw(testContract, 'panic_test', '');
t.deepEqual(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError,
'Smart contract panicked: explicit guest panic'
);
t.assert(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError
.match(/^Smart contract panicked:*/));

r = await ali.callRaw(testContract, 'panic_ascii_test', '');
t.deepEqual(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError,
'Smart contract panicked: abc'
t.assert(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError
.match(/^Smart contract panicked: abc*/)
);

r = await ali.callRaw(testContract, 'panic_js_number', '');
t.deepEqual(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError,
'Smart contract panicked: 356'
t.assert(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError
.match(/^Smart contract panicked: 356*/)
);

r = await ali.callRaw(testContract, 'panic_js_undefined', '');
t.deepEqual(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError,
'Smart contract panicked: explicit guest panic'
t.assert(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError
.match(/^Smart contract panicked:*/)
);

r = await ali.callRaw(testContract, 'panic_js_null', '');
t.deepEqual(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError,
'Smart contract panicked: null'
t.assert(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError
.match(/^Smart contract panicked: null*/)
);

r = await ali.callRaw(testContract, 'panic_utf8_test', '');
t.deepEqual(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError,
'Smart contract panicked: 水'
t.assert(
r.result.receipts_outcome[0].outcome.status.Failure.ActionError.kind.FunctionCallError.ExecutionError
.match(/Smart contract panicked: 水*/)
);

r = await ali.callRaw(testContract, 'panicUtf8_valid_utf8_sequence', '');
Expand Down
12 changes: 6 additions & 6 deletions tests/src/bytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,27 @@ export function storage_read_utf8_sequence_bytes_key_arbitrary_bytes_value() {
}

export function panic_test() {
near.panic()
throw Error()
}

export function panic_ascii_test() {
near.panic('abc')
throw Error('abc')
}

export function panic_js_number() {
near.panic(356)
throw Error(356)
}

export function panic_js_undefined() {
near.panic(undefined)
throw Error(undefined)
}

export function panic_js_null() {
near.panic(null)
throw Error(null)
}

export function panic_utf8_test() {
near.panic('水')
throw Error('水')
}

export function panicUtf8_valid_utf8_sequence() {
Expand Down
12 changes: 6 additions & 6 deletions tests/src/log_panic_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,27 @@ export function log_invalid_utf16_sequence_test() {
}

export function panic_test() {
near.panic()
throw Error()
}

export function panic_ascii_test() {
near.panic('abc')
throw Error('abc')
}

export function panic_js_number() {
near.panic(356)
throw Error(356)
}

export function panic_js_undefined() {
near.panic(undefined)
throw Error(undefined)
}

export function panic_js_null() {
near.panic(null)
throw Error(null)
}

export function panic_utf8_test() {
near.panic('水')
throw Error('水')
}

export function panicUtf8_valid_utf8_sequence() {
Expand Down

0 comments on commit 1db2e87

Please sign in to comment.