Skip to content
This repository has been archived by the owner on Feb 20, 2024. It is now read-only.

Commit

Permalink
Improve error on ProcessInvalid (#85)
Browse files Browse the repository at this point in the history
* Improve error on ProcessInvalid

* Make tests great again
  • Loading branch information
mattdean-digicatapult authored Mar 17, 2023
1 parent 1cf0c31 commit 5db5d32
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 19 deletions.
8 changes: 8 additions & 0 deletions app/api-v3/routes/run-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import logger from '../../logger.js'
import { validateInputIds, processRoles, processMetadata, validateProcess } from '../../util/appUtil.js'
import { getDefaultSecurity } from '../../util/auth.js'
import env from '../../env.js'
import { ExtrinsicError } from '../../util/errors.js'

const { PROCESS_IDENTIFIER_LENGTH } = env

Expand Down Expand Up @@ -84,6 +85,13 @@ export default function (apiService) {
try {
result = await apiService.runProcess(process, request.inputs, outputs)
} catch (err) {
if (err instanceof ExtrinsicError) {
res.status(err.code).json({
message: err.message,
})
return
}

logger.error(`Unexpected error running process: ${err}`)
res.status(500).json({
message: `Unexpected error processing items`,
Expand Down
12 changes: 11 additions & 1 deletion app/util/appUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const bs58 = basex(BASE58)
import env from '../env.js'
import logger from '../logger.js'
import { substrateApi as api, keyring } from './substrateApi.js'
import { ExtrinsicError } from './errors.js'

const {
USER_URI,
Expand Down Expand Up @@ -226,6 +227,15 @@ export async function getMembers() {
return membersRaw.map((m) => m.toString())
}

function ProcessExtrinsicError(error) {
if (!error.isModule) {
return new ExtrinsicError('Unknown', 500)
}

const decoded = api.registry.findMetaError(error.asModule)
return new ExtrinsicError(decoded.name, decoded.name === 'ProcessInvalid' ? 400 : 500)
}

export async function runProcess(process, inputs, outputs) {
if (inputs && outputs) {
await api.isReady
Expand All @@ -247,7 +257,7 @@ export async function runProcess(process, inputs, outputs) {
.map(({ event: { data } }) => data[0])

if (errors.length > 0) {
reject('ExtrinsicFailed error in simpleNFT')
reject(ProcessExtrinsicError(errors[0]))
}

const tokens = result.events
Expand Down
10 changes: 10 additions & 0 deletions app/util/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export class ExtrinsicError extends Error {
error
code

constructor(errorType, code) {
super(`Error processing extrinsic: ${errorType}`)
this.error = errorType
this.code = code
}
}
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dscp-api",
"version": "5.0.0",
"version": "5.0.1",
"description": "DSCP API",
"type": "module",
"repository": {
Expand Down
19 changes: 10 additions & 9 deletions test/helper/substrateHelper.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { before, after } from 'mocha'
import { substrateApi as api, keyring } from '../../app/util/substrateApi.js'

export const withNewTestProcess = (process) => {
const processStr = 'test-process'
export const withNewTestProcess = (
process,
restrictions = [
{
Restriction: 'None',
},
]
) => {
const processStr = process.name || 'test-process'
const buffer = Buffer.from(processStr, 'utf8')
const processId = `0x${buffer.toString('hex')}`
let processVersion
Expand All @@ -14,13 +21,7 @@ export const withNewTestProcess = (process) => {
const newProcess = await new Promise((resolve) => {
let unsub = null
api.tx.sudo
.sudo(
api.tx.processValidation.createProcess(processId, [
{
Restriction: 'None',
},
])
)
.sudo(api.tx.processValidation.createProcess(processId, restrictions))
.signAndSend(sudo, (result) => {
if (result.status.isInBlock) {
const { event } = result.events.find(({ event: { method } }) => method === 'ProcessCreated')
Expand Down
5 changes: 5 additions & 0 deletions test/integration/regressions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { getItemRoute, getLastTokenIdRoute } from '../helper/routeHelper.js'
const USER_ALICE_TOKEN = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'
import { indexToRole } from '../../app/util/appUtil.js'
import env from '../../app/env.js'
import { withNewTestProcess } from '../helper/substrateHelper.js'

const { API_MAJOR_VERSION, AUTH_ISSUER, AUTH_AUDIENCE, AUTH_TYPE } = env
const describeAuthOnly = AUTH_TYPE === 'JWT' ? describe : describe.skip
Expand All @@ -26,6 +27,7 @@ describeAuthOnly('Bug regression tests', function () {
let jwksMock
let authToken
let statusHandler
let process = {}

before(async () => {
nock.disableNetConnect()
Expand All @@ -50,6 +52,8 @@ describeAuthOnly('Bug regression tests', function () {
})
})

withNewTestProcess(process)

after(async function () {
await jwksMock.stop()
})
Expand All @@ -70,6 +74,7 @@ describeAuthOnly('Bug regression tests', function () {
.field(
'request',
JSON.stringify({
process,
inputs: [],
outputs,
})
Expand Down
17 changes: 12 additions & 5 deletions test/integration/routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ describe('routes', function () {
let authToken
let statusHandler
const process = {}
const failProcess = { name: 'fail' }

before(async function () {
const server = await createHttpServer()
Expand All @@ -301,6 +302,7 @@ describe('routes', function () {
})

withNewTestProcess(process)
withNewTestProcess(failProcess, [{ Restriction: 'Fail' }])

describe('happy path', function () {
test('add and get item - single metadata FILE', async function () {
Expand Down Expand Up @@ -338,7 +340,6 @@ describe('routes', function () {
const getItemResult = await getItemRoute(app, authToken, { id: firstTokenId + 1 })
expect(getItemResult.status).to.equal(200)
expect(getItemResult.body.id).to.equal(firstTokenId + 1)
expect(getItemResult.body.original_id).to.equal(firstTokenId)
})

test('add and get item - single metadata LITERAL', async function () {
Expand Down Expand Up @@ -552,7 +553,7 @@ describe('routes', function () {
metadata: new Map([[key, { File: base64Metadata }]]),
}

await runProcess(null, [], [output])
await runProcess(process, [], [output])

await getItemRoute(app, authToken, { id: lastToken.body.id + 1 })

Expand Down Expand Up @@ -871,7 +872,7 @@ describe('routes', function () {
metadata: { testNone: { type: 'NONE' } },
},
]
await postRunProcess(app, authToken, [], outputs)
await postRunProcess(app, authToken, process, [], outputs)

const actualResult = await postRunProcess(app, authToken, process, [lastTokenId + 1], outputs)

Expand All @@ -889,7 +890,7 @@ describe('routes', function () {
metadata: { testNone: { type: 'NONE' } },
},
]
await postRunProcess(app, authToken, [], outputs)
await postRunProcess(app, authToken, process, [], outputs)

const firstBurn = await postRunProcess(app, authToken, process, [lastTokenId + 1], outputs)
expect(firstBurn.status).to.equal(200)
Expand Down Expand Up @@ -1060,6 +1061,12 @@ describe('routes', function () {
expect(runProcessResult.status).to.equal(400)
expect(runProcessResult.body.message).to.equal(`Invalid process version: ${version}`)
})

test('invalid inputs for process', async function () {
const runProcessResult = await postRunProcess(app, authToken, failProcess, [], [])
expect(runProcessResult.status).to.equal(400)
expect(runProcessResult.body.message).to.equal(`Error processing extrinsic: ProcessInvalid`)
})
})
})

Expand All @@ -1080,7 +1087,7 @@ describe('routes', function () {

withNewTestProcess(process)

describe.only('happy path', function () {
describe('happy path', function () {
test('add and get item metadata - FILE + LITERAL + TOKEN_ID + NONE', async function () {
const outputs = [
{
Expand Down
2 changes: 1 addition & 1 deletion test/test.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
LOG_LEVEL=trace
LOG_LEVEL=fatal

PORT=3001
API_HOST=localhost
Expand Down

0 comments on commit 5db5d32

Please sign in to comment.