@csvw-rdf-convertor/core
Version:
This library was generated with [Nx](https://nx.dev).
89 lines (88 loc) • 2.93 kB
JavaScript
export class ValidationError extends Error {
constructor(message, location){
super(message);
this.location = location;
this.name = 'ValidationError';
}
}
/**
* IssueTracker is a utility class for tracking issues (errors and warnings) during the validation process.
* It can throw errors or collect them for later retrieval.
*/ export class IssueTracker {
setDefaults(options) {
if (!options) options = {};
var _options_throwErrors, _options_collectIssues;
return {
eventEmitter: options.eventEmitter,
throwErrors: (_options_throwErrors = options.throwErrors) != null ? _options_throwErrors : true,
collectIssues: (_options_collectIssues = options.collectIssues) != null ? _options_collectIssues : true
};
}
/**
* Adds an error message to the issue tracker.
* @param message - The error message to be added.
* @param withLocation - should the error message include the location information?
*/ addError(message, withLocation = true) {
if (this.options.throwErrors) {
if (withLocation && this.location.hasLocation) {
throw new ValidationError(message, this.location.value);
}
throw new ValidationError(message);
}
const error = {
type: 'error',
message
};
if (withLocation && this.location.hasLocation) {
error.location = this.location.value;
}
if (this.options.collectIssues) {
this.errors.push(error);
}
if (this.options.eventEmitter) {
this.options.eventEmitter.emit('error', error);
}
}
/**
* Adds a warning message to the issue tracker.
* @param message - The warning message to be added.
* @param withLocation - should the warning message include the location information?
*/ addWarning(message, withLocation = true) {
const warning = {
type: 'warning',
message
};
if (withLocation && this.location.hasLocation) {
warning.location = this.location.value;
}
if (this.options.collectIssues) {
this.warnings.push(warning);
}
if (this.options.eventEmitter) {
this.options.eventEmitter.emit('warning', warning);
}
}
/**
* Gets all collected errors
*/ getErrors() {
return this.errors;
}
/**
* Gets all collected warnings
*/ getWarnings() {
return this.warnings;
}
/**
* Clears all collected errors and warnings.
*/ clearAll() {
this.errors = [];
this.warnings = [];
}
constructor(location, options){
this.location = location;
this.errors = [];
this.warnings = [];
this.options = this.setDefaults(options);
}
}
//# sourceMappingURL=issue-tracker.js.map