Skip to content

Commit

Permalink
fix(compiler errors): construct error message by server response
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Oct 29, 2021
1 parent 6d9003b commit 8621352
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
20 changes: 19 additions & 1 deletion src/contract/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,25 @@ export default AsyncInit.compose(ContractBase, {
compilerUrl = compilerUrl.replace(/\/$/, '')
const client = await genSwaggerClient(`${compilerUrl}/api`, {
disableBigNumbers: true,
disableCaseConversion: true
disableCaseConversion: true,
responseInterceptor: response => {
if (response.ok) return
let message = `${new URL(response.url).pathname.slice(1)} error`
if (response.body.reason) {
message += ': ' + response.body.reason +
(response.body.parameter ? ` in ${response.body.parameter}` : '') +
// TODO: revising after improving documentation https://github.com/aeternity/aesophia_http/issues/78
(response.body.info ? ` (${JSON.stringify(response.body.info)})` : '')
}
if (Array.isArray(response.body)) {
message += ':\n' + response.body
.map(e => `${e.type}:${e.pos.line}:${e.pos.col}: ${e.message}${e.context ? `(${e.context})` : ''}`)
.map(e => e.trim()) // TODO: remove after fixing https://github.com/aeternity/aesophia_http/issues/80
.join('\n')
}
response.statusText = message
return response
}
})
this.compilerVersion = client.spec.info.version
this._compilerApi = client.api
Expand Down
9 changes: 7 additions & 2 deletions src/utils/swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ let warnedAboutInternalApiUsage = false
* @param {String} [options.internalUrl] - Node internal URL
* @param {Boolean} [options.disableBigNumbers]
* @param {Boolean} [options.disableCaseConversion]
* @param {Function} [options.responseInterceptor]
* @return {Object} Swagger client
* @example (await genSwaggerClient('https://mainnet.aeternity.io/api')).getAccountByPubkey('ak_jupBUgZNbcC4krDLR3tAkw1iBZoBbkNeShAq4atBtpFWmz36r')
*/
export default async (specUrl, { spec, internalUrl, disableBigNumbers, disableCaseConversion } = {}) => {
export default async (
specUrl,
{ spec, internalUrl, disableBigNumbers, disableCaseConversion, responseInterceptor } = {}
) => {
spec = spec || await (await fetch(specUrl)).json()
const jsonImp = disableBigNumbers ? JSON : JsonBig

Expand All @@ -64,9 +68,10 @@ export default async (specUrl, { spec, internalUrl, disableBigNumbers, disableCa
responseInterceptor: response => {
if (!response.text) return response
const body = jsonImp.parse(response.text)
return Object.assign(response, {
Object.assign(response, {
body: disableCaseConversion ? body : pascalizeKeys(body)
})
return (responseInterceptor && responseInterceptor(response)) || response
}
})
}))
Expand Down
21 changes: 21 additions & 0 deletions test/integration/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,22 @@ describe('Contract', function () {
isString.should.be.equal(true)
})

it('throws clear exception if compile broken contract', async () => {
await expect(sdk.compileContractAPI(
'contract Foo =\n' +
' entrypoint getArg(x : bar) = x\n' +
' entrypoint getArg(x : int) = baz\n' +
' entrypoint getArg1(x : int) = baz\n'
)).to.be.rejectedWith(
'compile error:\n' +
'type_error:3:3: Duplicate definitions of getArg at\n' +
' - line 2, column 3\n' +
' - line 3, column 3\n' +
'type_error:3:32: Unbound variable baz at line 3, column 32\n' +
'type_error:4:33: Unbound variable baz at line 4, column 33'
)
})

it('Get FATE assembler', async () => {
const result = await sdk.getFateAssembler(bytecode)
result.should.be.a('object')
Expand All @@ -419,6 +435,11 @@ describe('Contract', function () {
aci.should.have.property('interface')
})

it('throws clear exception if generating ACI with no arguments', async () => {
await expect(sdk.contractGetACI())
.to.be.rejectedWith('validation_error in body ({"error":"missing_required_property","data":"code","path":[]})')
})

it('encode call-data', async () => {
callData = await sdk.contractEncodeCallDataAPI(identityContract, 'init', [])
const prefix = callData.slice(0, 2)
Expand Down

0 comments on commit 8621352

Please sign in to comment.