Skip to content

Conversation

@DebottamMandal123
Copy link

Fixes #8656
Improves exception handling during schema import operations to provide clearer and more actionable error messages.

Summary of Changes

1. ECDb Error Improvements (core/backend/src/ECDb.ts):

  • Added pre-validation for schema file existence (fs.existsSync / fs.accessSync)

  • Throw structured IModelStatus.FileNotFound errors including file path context

  • Wrapped importSchema native call with contextual exception handling

2. IModelDb Enhancements (core/backend/src/IModelDb.ts):

  • Introduced createSchemaImportError helper for consistent error messaging

  • Pre-validation for imported schema file paths

  • Improved context propagation for:

        - File load failures
    
        - XML import failures
    
        - Schema sync failures
    

3. Regression Fix (core/backend/src/test/hubaccess/Rebase.test.ts):

  • Corrected incorrect usage of importSchemas in existing test

  • Now writes schema XML to a temp file before import

4. Added New Tests:

  • New suite: core/backend/src/test/standalone/SchemaImportErrors.test.ts

-Covers:

      - Missing file path → FileNotFound

      - Invalid schema content → detailed contextual error message

Validation:

1. Automated:

     - rush build 

     - Test suites (new + updated) —  All passed

Manual:

     - Verified clear messaging on missing file scenarios
📸 Screenshot of test results Improved-exception-messages-testing_image

@DebottamMandal123 DebottamMandal123 requested a review from a team as a code owner November 23, 2025 21:33
@CLAassistant
Copy link

CLAassistant commented Nov 23, 2025

CLA assistant check
All committers have signed the CLA.

@kabentley
Copy link
Collaborator

I suggest you instead use this opportunity to introduce a new ITwinError interface for schema related errors instead of propagation the ErrorNumber insanity.

const schemaPart = `Schema(s): ${schemaSummary}.`;
const message = nativeMessage ? `${base} ${schemaPart} ${nativeMessage}` : `${base} ${schemaPart}`;

Logger.logError(loggerCategory, message, () => ({
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do not log errors when an exception is thrown. Log them where it is caught.

Logger.logError(loggerCategory, `Failed to import schema from '${pathName}'.`);
throw new IModelError(status, `Failed to import schema from '${pathName}'.`);
const errMsg = `Schema import failed while importing/validating schema. Schema: ${pathName}. (status ${status})`;
Logger.logError(loggerCategory, errMsg, () => ({ pathName, status }));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Logger.logError(loggerCategory, errMsg, () => ({ pathName, status }));

When you log, there's no need for the 3rd argument to be a function. Instead pass the metadata object.


if (!fs.existsSync(pathName)) {
const errMsg = `Schema import failed while loading schema file. Schema: ${pathName}. File does not exist.`;
Logger.logError(loggerCategory, errMsg, () => ({ pathName }));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't log here. Callers should log when they catch the exception.

fs.accessSync(pathName, fs.constants.R_OK);
} catch (err) {
const errMsg = `Schema import failed while loading schema file. Schema: ${pathName}. ${(err as Error).message}`;
Logger.logError(loggerCategory, errMsg, () => ({ pathName }));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't log here.


const loggerCategory: string = BackendLoggerCategory.IModelDb;

function summarizeSchemaSources(schemaSources?: readonly string[]): string {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks to me like you need to move all the schema related code to a new file. Polluting IModelDb.ts isn't a good idea.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for your feedback, I will make the suggested changes and update the pr shortly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve exception message on failed schema imports

3 participants