Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve error handling in TraceManager with TraceError class #5603

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions libs/remix-debug/src/trace/traceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import { TraceCache } from './traceCache'
import { TraceStepManager } from './traceStepManager'
import { isCreateInstruction } from './traceHelper'

export class TraceError extends Error {
constructor(message: string, public readonly stepIndex?: number) {
super(message)
this.name = 'TraceError'
Object.setPrototypeOf(this, TraceError.prototype)
}
}

export class TraceManager {
web3
fork: string
Expand Down Expand Up @@ -126,38 +134,42 @@ export class TraceManager {
try {
this.checkRequestedStep(stepIndex)
} catch (check) {
throw new Error(check)
throw new TraceError(`Invalid step index: ${check.message}`, stepIndex)
}
const callsPath = util.buildCallPath(stepIndex, this.traceCache.callsTree.call)
if (callsPath === null) throw new Error('no call path built')
return callsPath
if (callsPath) {
return callsPath
} else {
throw new TraceError('Failed to build call path: no valid path found', stepIndex)
}
}

getCallStackAt (stepIndex) {
try {
this.checkRequestedStep(stepIndex)
} catch (check) {
throw new Error(check)
throw new TraceError(`Invalid step index: ${check.message}`, stepIndex)
}
const call = util.findCall(stepIndex, this.traceCache.callsTree.call)
if (call === null) {
throw new Error('no callstack found')
if (call) {
return call.callStack
} else {
throw new TraceError('No callstack found for the given step index', stepIndex)
}
return call.callStack
}

getStackAt (stepIndex) {
this.checkRequestedStep(stepIndex)
if (this.trace[stepIndex] && this.trace[stepIndex].stack) { // there's always a stack
try {
this.checkRequestedStep(stepIndex)
} catch (check) {
throw new TraceError(`Invalid step index: ${check.message}`, stepIndex)
}
if (this.trace[stepIndex] && this.trace[stepIndex].stack) {
if (Array.isArray(this.trace[stepIndex].stack)) {
const stack = this.trace[stepIndex].stack.slice(0)
stack.reverse()
return stack.map(el => toHexPaddedString(el))
} else {
// it's an object coming from the VM.
// for performance reasons,
// we don't turn the stack coming from the VM into an array when the tx is executed
// but now when the app needs it.
const stack = []
for (const prop in this.trace[stepIndex].stack) {
if (prop !== 'length') {
Expand All @@ -167,9 +179,8 @@ export class TraceManager {
stack.reverse()
return stack
}
} else {
throw new Error('no stack found')
}
throw new TraceError('No stack data found for the given step index', stepIndex)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to be in a else statement.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yann300 made an update, and also provided a similar change to another place in code. Does it seem alright to you?

}

getLastCallChangeSince (stepIndex) {
Expand Down