Skip to main content

Get model errors

Get model errors

Errors for a model can be obtained from the Errors property of the IModel object. You can also use the HasError property of the IModel object to check whether there are errors, and the HasErrorWithChildren property of the IModel object to check whether there are errors including child elements.

public void GetErrors(ICommandContext c, ICommandParams p) 
{
IModel model = c.App.Workspace.CurrentModel;

//If there is an error
if ( model.HasError )
{
//Output the error message
foreach ( IError error in model.Errors )
{
c.App.Output.WriteLine("sample", $"Error: {error.Message}");
}
}
}

Get all errors

To get all current errors, use the Errors property of the IApplication object.

public void ListErrors(ICommandContext c, ICommandParams p) 
{
IErrors errors = c.App.Errors;

//Access all errors
foreach ( var error in errors.AllErrors)
{
c.App.Output.WriteLine("sample", $"Error: {error.Message}");
}
}

Get a specific type of error

To get a specific type of error, use the various properties of the IErrors object. The available properties are as follows. All types are collections of errors (IErrorCollection).

  • AllErrors: Gets all error information.
  • Errors: Gets error information with the error type "Error".
  • Warnings: Gets error information with the error type "Warning".
  • Informations: Gets error information with the error type "Information".
  • Summaries: Gets error information with the error type "Summary".

public void ListErrors(ICommandContext c, ICommandParams p)
{
IErrors errors = c.App.Errors;

//Gets all information including warnings
var allErrors = errors.AllErrors;

//Gets only information with type error
var onlyErrors = errors.Errors;

//Gets only information with type warning
var onlyWarnings = errors.Warnings;
}