diff --git a/examples/src/cross-contract-call.js b/examples/src/cross-contract-call.js index 8a713064a..a315bfe97 100644 --- a/examples/src/cross-contract-call.js +++ b/examples/src/cross-contract-call.js @@ -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}`) diff --git a/examples/src/non-fungible-token.js b/examples/src/non-fungible-token.js index 980163342..77d677f48 100644 --- a/examples/src/non-fungible-token.js +++ b/examples/src/non-fungible-token.js @@ -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'}`) diff --git a/jsvm/tests/src/bytes.js b/jsvm/tests/src/bytes.js index c29269c13..a10d1ac3c 100644 --- a/jsvm/tests/src/bytes.js +++ b/jsvm/tests/src/bytes.js @@ -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() { diff --git a/lib/api.d.ts b/lib/api.d.ts index aaedb3ceb..709d89c15 100644 --- a/lib/api.d.ts +++ b/lib/api.d.ts @@ -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; diff --git a/lib/api.js b/lib/api.js index 0f7ec0b38..983cad0f0 100644 --- a/lib/api.js +++ b/lib/api.js @@ -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); } @@ -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) { diff --git a/src/api.ts b/src/api.ts index 2da516a82..3676aef14 100644 --- a/src/api.ts +++ b/src/api.ts @@ -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; } @@ -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); @@ -424,7 +417,7 @@ export function promiseResult( ) { return status; } else { - panic(`Unexpected return code: ${status}`); + throw Error(`Unexpected return code: ${status}`); } } diff --git a/tests/__tests__/bytes.ava.js b/tests/__tests__/bytes.ava.js index 80443bb30..c2554de47 100644 --- a/tests/__tests__/bytes.ava.js +++ b/tests/__tests__/bytes.ava.js @@ -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', ''); diff --git a/tests/__tests__/test_log_panic_api.ava.js b/tests/__tests__/test_log_panic_api.ava.js index 7bf0aa7a6..59700165b 100644 --- a/tests/__tests__/test_log_panic_api.ava.js +++ b/tests/__tests__/test_log_panic_api.ava.js @@ -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', ''); diff --git a/tests/src/bytes.js b/tests/src/bytes.js index 2d9123687..6f35221b7 100644 --- a/tests/src/bytes.js +++ b/tests/src/bytes.js @@ -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() { diff --git a/tests/src/log_panic_api.js b/tests/src/log_panic_api.js index 56935a921..c87e5ee47 100644 --- a/tests/src/log_panic_api.js +++ b/tests/src/log_panic_api.js @@ -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() {