-
Notifications
You must be signed in to change notification settings - Fork 228
Description
I see three main code flows where we are throwing errors from some methods and then branching on those errors inside catch block.
- The first place is here https://github.com/iTwin/itwinjs-core/blob/soham-bentley/debugging-exceptions/core/backend/src/IModelDb.ts#L1286-L1294
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
getClass can internally throw error from generateClass
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.
resolveElementKeymethod ofIModelDb.tsitself throws errors and also callsresolveInstanceKeymethod ofnativeDb(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 (
itwinjs-core/core/backend/src/IModelDb.ts
Line 2240 in c760c18
| const instanceKey = this.resolveElementKey(props); |
AND
by
queryElementIdByCode method (itwinjs-core/core/backend/src/IModelDb.ts
Line 2315 in c760c18
| 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
- Similarly
resolveInstanceKeymethod ofnativeDbis also referenced byresolveModelKeymethod.() which is in turn referenced byitwinjs-core/core/backend/src/IModelDb.ts
Line 1990 in c760c18
private resolveModelKey(modelIdArg: ModelLoadProps): IModelJsNative.ResolveInstanceKeyResult { tryGetModelPropsmethod ()itwinjs-core/core/backend/src/IModelDb.ts
Line 1917 in c760c18
public tryGetModelProps<T extends ModelProps>(id: Id64String): T | undefined {
Here also we do branching in catch block and return undefined.