Skip to content

iTwinJs Core using errors for basic code flow #8774

@ColinKerr

Description

@ColinKerr

I see three main code flows where we are throwing errors from some methods and then branching on those errors inside catch block.

  1. The first place is here https://github.com/iTwin/itwinjs-core/blob/soham-bentley/debugging-exceptions/core/backend/src/IModelDb.ts#L1286-L1294
Image

Here as we can see in the catch block if the error has the error number of IModelStatus.NotFound we branch on it and load meta data for this class first and then try to get the class again.
Upon inspection I found that
https://github.com/iTwin/itwinjs-core/blob/soham-bentley/debugging-exceptions/core/backend/src/ClassRegistry.ts#L309-L312

Image

getClass can internally throw error from generateClass

https://github.com/iTwin/itwinjs-core/blob/soham-bentley/debugging-exceptions/core/backend/src/ClassRegistry.ts#L281-L292

Image

generateClass can throw errors from the following three lines

throw this.makeMetaDataNotFoundError(classFullName);

this.getClass(metadata.baseClasses[0], iModel);

return this.generateClassForEntity(metadata, iModel);

Among these three errors , when the metadata for that particular className comes as undefined we try to handle it manually inside the catch block (https://github.com/iTwin/itwinjs-core/blob/soham-bentley/debugging-exceptions/core/backend/src/IModelDb.ts#L1286-L1294) by loading the metadata and trying again...the second time the metadata will surely not be undefined.
Upon close inspection we can understand that other than this specific error, the other errors which are thrown from generateClass will be thrown the second time as well when we call getClass the second time from the catch block and therefore the other errors are treated and respected as errors and bubbled up the chain.

This complex workflow for simple metadata loading leads to many false positive errors. I believe there is a more and simple and elegant way to handle this.

  1. resolveElementKey method of IModelDb.ts itself throws errors and also calls resolveInstanceKey method of nativeDb(https://github.com/iTwin/itwinjs-core/blob/soham-bentley/debugging-exceptions/core/backend/src/IModelDb.ts#L2186-L2215) which also internally throws errors on the native side.

resolveElementKey method is being referenced by tryGetElementProps method (

const instanceKey = this.resolveElementKey(props);
)
AND
by queryElementIdByCode method (
const elementKey = this.resolveElementKey(codeToQuery);
)

Here also in both the methods tryGetElementProps and queryElementIdByCode we capture the erros in catch block and branch on it..but the branching logic is quite different in case of both methods

  1. Similarly resolveInstanceKey method of nativeDb is also referenced by resolveModelKey method.(
    private resolveModelKey(modelIdArg: ModelLoadProps): IModelJsNative.ResolveInstanceKeyResult {
    ) which is in turn referenced by tryGetModelProps method (
    public tryGetModelProps<T extends ModelProps>(id: Id64String): T | undefined {
    )
    Here also we do branching in catch block and return undefined.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions