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

Commit

Permalink
Merge pull request #1945 from trufflesuite/cannot-locate
Browse files Browse the repository at this point in the history
Add error for failure to locate struct or enum during storage allocation
  • Loading branch information
haltman-at committed Apr 24, 2019
2 parents f967b17 + ec375dc commit c410331
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/truffle-decode-utils/src/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export namespace Definition {
return definition.typeDescriptions.typeIdentifier;
}

export function typeString(definition: AstDefinition): string {
return definition.typeDescriptions.typeString;
}

/**
* returns basic type class for a variable definition node
* e.g.:
Expand Down
10 changes: 9 additions & 1 deletion packages/truffle-decoder/lib/allocate/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const debug = debugModule("decoder:allocate:storage");
import { StoragePointer } from "../types/pointer";
import { StorageAllocations, StorageAllocation, StorageMemberAllocations } from "../types/allocation";
import { StorageLength, isWordsLength, Range } from "../types/storage";
import { UnknownBaseContractIdError } from "../types/errors";
import { UnknownBaseContractIdError, UnknownUserDefinedTypeError } from "../types/errors";
import { AstDefinition, AstReferences } from "truffle-decode-utils";
import { readDefinition } from "../read/constant"
import * as DecodeUtils from "truffle-decode-utils";
Expand Down Expand Up @@ -218,6 +218,10 @@ function storageSizeAndAllocate(definition: AstDefinition, referenceDeclarations
//should never need to worry about faked-up enum definitions, so just
//checking the referencedDeclaration field would also work
const referenceDeclaration: AstDefinition = referenceDeclarations[referenceId];
if(referenceDeclaration === undefined) {
let typeString = DecodeUtils.Definition.typeString(definition);
throw new UnknownUserDefinedTypeError(referenceId, typeString);
}
const numValues: number = referenceDeclaration.members.length;
return [{bytes: Math.ceil(Math.log2(numValues) / 8)}, existingAllocations];
}
Expand Down Expand Up @@ -280,6 +284,10 @@ function storageSizeAndAllocate(definition: AstDefinition, referenceDeclarations
if(allocation === undefined) {
//if we don't find an allocation, we'll have to do the allocation ourselves
const referenceDeclaration: AstDefinition = referenceDeclarations[referenceId];
if(referenceDeclaration === undefined) {
let typeString = DecodeUtils.Definition.typeString(definition);
throw new UnknownUserDefinedTypeError(referenceId, typeString);
}
debug("definition %O", definition);
allocations = allocateStruct(referenceDeclaration, referenceDeclarations, existingAllocations);
allocation = allocations[referenceId];
Expand Down
13 changes: 13 additions & 0 deletions packages/truffle-decoder/lib/types/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,16 @@ export class UnknownBaseContractIdError extends Error {
this.baseId = baseId;
}
}

export class UnknownUserDefinedTypeError extends Error {
public name: string;
public typeString: string;
public id: number;
constructor(id: number, typeString: string) {
const message = `Cannot locate definition for ${typeString}$ (ID ${id})`;
super(message);
this.name = "UnknownUserDefinedTypeError";
this.id = id;
this.typeString = typeString;
}
}

0 comments on commit c410331

Please sign in to comment.