126 lines
3.9 KiB
TypeScript
126 lines
3.9 KiB
TypeScript
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
// Copyright (C) 2026 ZiroEDA and contributors.
|
|
// Portions derived from KiCad, copyright The KiCad Developers. See NOTICE.md.
|
|
/**
|
|
* `include/ki_exception.h` / `common/exceptions.cpp`: the exception classes
|
|
* the file readers throw. `PARSE_ERROR` lives with the lexer in
|
|
* `dsnlexer.ts` (its one constructor site) and is re-exported here.
|
|
*/
|
|
|
|
import { PARSE_ERROR } from './dsnlexer.js';
|
|
|
|
export { PARSE_ERROR };
|
|
|
|
/**
|
|
* Hold a translatable error message and may be used when throwing exceptions
|
|
* containing a translated error message.
|
|
*/
|
|
export class KI_PARAM_ERROR extends Error {
|
|
constructor(aMessage = '') {
|
|
super(aMessage);
|
|
this.name = 'KI_PARAM_ERROR';
|
|
}
|
|
|
|
What(): string {
|
|
return this.message;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Hold an error message and may be used when throwing exceptions containing meaningful
|
|
* error messages.
|
|
*/
|
|
export class IO_ERROR extends Error {
|
|
protected problem: string;
|
|
protected where: string;
|
|
|
|
constructor(aProblem = '', aWhere = '') {
|
|
super(aProblem);
|
|
this.name = 'IO_ERROR';
|
|
this.problem = aProblem;
|
|
this.where = aWhere;
|
|
}
|
|
|
|
/** what was the problem? */
|
|
Problem(): string {
|
|
return this.problem;
|
|
}
|
|
|
|
/** where did the Problem() occur? */
|
|
Where(): string {
|
|
return this.where;
|
|
}
|
|
|
|
/** A composite of Problem() and Where() */
|
|
What(): string {
|
|
return this.where === '' ? this.problem : `${this.problem}\n${this.where}`;
|
|
}
|
|
}
|
|
|
|
/** `THROW_IO_ERROR( msg )` (ki_exception.h). The file, function and line are not kept. */
|
|
export function THROW_IO_ERROR(aMessage: string): never {
|
|
throw new IO_ERROR(aMessage);
|
|
}
|
|
|
|
/**
|
|
* Variant of #PARSE_ERROR indicating that a syntax or related error was likely caused
|
|
* by a file generated by a newer version of KiCad than this. This can be used to generate
|
|
* more useful messages.
|
|
*/
|
|
export class FUTURE_FORMAT_ERROR extends PARSE_ERROR {
|
|
requiredVersion: string; ///< version or date of KiCad required to open file
|
|
requiredGenerator: string; ///< version or date of KiCad required to open file
|
|
|
|
constructor(aRequiredVersion: string, aRequiredGenerator?: string);
|
|
constructor(aParseError: PARSE_ERROR, aRequiredVersion: string, aRequiredGenerator?: string);
|
|
constructor(a: string | PARSE_ERROR, b = '', c = '') {
|
|
const aRequiredVersion = typeof a === 'string' ? a : b;
|
|
const aRequiredGenerator = typeof a === 'string' ? b : c;
|
|
const aParseError = typeof a === 'string' ? null : a;
|
|
|
|
let requiredVersion = aRequiredVersion;
|
|
let requiredGenerator = aRequiredGenerator;
|
|
let problem: string;
|
|
|
|
const init = (): string => {
|
|
if (requiredGenerator === '') {
|
|
return (
|
|
'KiCad was unable to open this file because it was created with a more ' +
|
|
'recent version than the one you are running.\n\n' +
|
|
`To open it you will need to upgrade KiCad to a version dated ${requiredVersion} or ` +
|
|
'later.'
|
|
);
|
|
}
|
|
|
|
return (
|
|
'KiCad was unable to open this file because it was created with a more ' +
|
|
'recent version than the one you are running.\n\n' +
|
|
`To open it you will need to upgrade KiCad to version ${requiredGenerator} or ` +
|
|
`later (file format dated ${requiredVersion} or later).`
|
|
);
|
|
};
|
|
|
|
if (aParseError instanceof FUTURE_FORMAT_ERROR) {
|
|
requiredVersion = aParseError.requiredVersion;
|
|
requiredGenerator = aParseError.requiredGenerator;
|
|
problem = aParseError.message;
|
|
} else {
|
|
problem = init();
|
|
|
|
if (aParseError && aParseError.message !== '')
|
|
problem += `\n\nFull error text:\n${aParseError.message}`;
|
|
}
|
|
|
|
super(
|
|
problem,
|
|
aParseError ? aParseError.source : '',
|
|
aParseError ? aParseError.inputLine : '',
|
|
aParseError ? aParseError.lineNumber : 0,
|
|
aParseError ? aParseError.byteIndex : 0,
|
|
);
|
|
this.name = 'FUTURE_FORMAT_ERROR';
|
|
this.requiredVersion = requiredVersion;
|
|
this.requiredGenerator = requiredGenerator;
|
|
}
|
|
}
|