@fs-eire/wgsl-template
Version:
A powerful template system for generating WGSL (WebGPU Shading Language) code with support for parameters, conditionals, and multiple output formats including C++ code generation.
152 lines • 4.58 kB
JavaScript
// ============================================================================
// WGSL Template Error Types
// ============================================================================
/**
* Base class for all WGSL template errors.
*/
export class WgslTemplateError extends Error {
/**
* The file path where the error occurred, if applicable.
*/
filePath;
/**
* The line number where the error occurred, if applicable.
*/
lineNumber;
/**
* The column number where the error occurred, if applicable.
*/
columnNumber;
constructor(message, options) {
super(message);
this.name = this.constructor.name;
this.filePath = options?.filePath;
this.lineNumber = options?.lineNumber;
this.columnNumber = options?.columnNumber;
// Preserve the cause chain
if (options?.cause) {
this.cause = options.cause;
}
}
/**
* Returns a formatted error message with location information.
*/
getFormattedMessage() {
let location = "";
if (this.filePath) {
location = this.filePath;
if (this.lineNumber !== undefined) {
location += `:${this.lineNumber}`;
if (this.columnNumber !== undefined) {
location += `:${this.columnNumber}`;
}
}
location = ` at ${location}`;
}
return `${this.message}${location}`;
}
}
/**
* Error that occurs during template loading (file system operations).
*/
export class WgslTemplateLoadError extends WgslTemplateError {
/**
* The type of load operation that failed.
*/
operation;
constructor(message, operation, options) {
super(message, options);
this.operation = operation;
}
}
/**
* Error that occurs during template parsing (syntax analysis, include resolution, etc.).
*/
export class WgslTemplateParseError extends WgslTemplateError {
/**
* The type of parse operation that failed.
*/
operation;
/**
* The source line that caused the error, if available.
*/
sourceLine;
constructor(message, operation, options) {
super(message, options);
this.operation = operation;
this.sourceLine = options?.sourceLine;
}
}
/**
* Error that occurs during code generation.
*/
export class WgslTemplateGenerateError extends WgslTemplateError {
/**
* The type of generation operation that failed.
*/
operation;
/**
* The name of the code pattern or parameter that caused the error, if applicable.
*/
symbolName;
/**
* The expected type vs actual type, if this is a type-related error.
*/
typeInfo;
constructor(message, operation, options) {
super(message, options);
this.operation = operation;
this.symbolName = options?.symbolName;
this.typeInfo = options?.typeInfo;
}
}
/**
* Error that occurs during the final build process (file writing, output generation).
*/
export class WgslTemplateBuildError extends WgslTemplateError {
/**
* The type of build operation that failed.
*/
operation;
/**
* The target output path that caused the error, if applicable.
*/
outputPath;
constructor(message, operation, options) {
super(message, options);
this.operation = operation;
this.outputPath = options?.outputPath;
}
}
/**
* Type guard to check if an error is a WGSL template error.
*/
export function isWgslTemplateError(error) {
return error instanceof WgslTemplateError;
}
/**
* Type guard functions for specific error types.
*/
export function isWgslTemplateLoadError(error) {
return error instanceof WgslTemplateLoadError;
}
export function isWgslTemplateParseError(error) {
return error instanceof WgslTemplateParseError;
}
export function isWgslTemplateGenerateError(error) {
return error instanceof WgslTemplateGenerateError;
}
export function isWgslTemplateBuildError(error) {
return error instanceof WgslTemplateBuildError;
}
/**
* Utility function to create an error from an unknown error with context.
*/
export function wrapError(ErrorClass, operation, originalError, context) {
const message = originalError instanceof Error ? originalError.message : String(originalError);
return new ErrorClass(message, operation, {
...context,
cause: originalError instanceof Error ? originalError : undefined,
});
}
//# sourceMappingURL=errors.js.map