AutoRest Core's message system provides structured communication between pipeline components with typed channels and comprehensive source location tracking.
Enumeration of all available message types for categorizing AutoRest communications.
/**
* Defines the types of messages that can be emitted during AutoRest processing
* Each channel represents a different severity or category of information
*/
enum Channel {
/** Mild responses, not necessarily actionable */
Information = "information",
/** Important for best practices, not catastrophic */
Warning = "warning",
/** Blocking issues that prevent successful operation */
Error = "error",
/** Internal AutoRest implementation details */
Debug = "debug",
/** Additional clarity on the process */
Verbose = "verbose",
/** Catastrophic failure, likely terminating process */
Fatal = "fatal",
/** Guidance without forcing action */
Hint = "hint",
/** File output from extension */
File = "file",
/** Configuration file update/creation */
Configuration = "configuration",
/** Path to not remove during clear-output-folder */
Protect = "protect",
/** Control messages for pipeline coordination */
Control = "Control"
}Core message structure used throughout the AutoRest pipeline.
/**
* Represents a message emitted during AutoRest processing
* Contains all necessary information for logging and error tracking
*/
interface Message {
/** The type/severity of the message */
Channel: Channel;
/** Optional hierarchical key for message categorization */
Key?: Iterable<string>;
/** Additional structured data associated with the message */
Details?: any;
/** Human-readable message text */
Text: string;
/** Source location information (injected by core) */
Source?: Array<SourceLocation>;
/** Name of the plugin that generated the message (injected by core) */
Plugin?: string;
/** Formatted message for display (injected by core) */
FormattedMessage?: string;
}Usage Examples:
import { Message, Channel } from "@autorest/core";
// Create information message
const infoMessage: Message = {
Channel: Channel.Information,
Text: "Processing OpenAPI specification",
Key: ["processing", "openapi"],
Details: { inputFile: "swagger.json" }
};
// Create error message with source location
const errorMessage: Message = {
Channel: Channel.Error,
Text: "Invalid schema reference",
Source: [{
document: "swagger.json",
Position: { line: 25, column: 10 }
}]
};
// Handle messages in logger sink
const loggerSink = (message: Message) => {
switch (message.Channel) {
case Channel.Error:
case Channel.Fatal:
console.error(`❌ ${message.Text}`);
break;
case Channel.Warning:
console.warn(`⚠️ ${message.Text}`);
break;
case Channel.Information:
console.log(`ℹ️ ${message.Text}`);
break;
case Channel.Debug:
if (process.env.DEBUG) {
console.debug(`🐛 ${message.Text}`);
}
break;
}
};Specialized message type for file generation events.
/**
* Specialized message for file artifacts generated during processing
* Extends base Message with artifact-specific details
*/
interface ArtifactMessage extends Message {
/** Artifact details including optional source map information */
Details: Artifact & {
sourceMap?: Array<Mapping> | RawSourceMap
};
}
interface Artifact {
/** File URI/path for the generated artifact */
uri: string;
/** Type/category of the artifact (e.g., "source-file-csharp") */
type: string;
/** Generated file content */
content: string;
}Source location information for precise error reporting and debugging.
/**
* Represents source location information for messages
* Used for precise error reporting and IDE integration
*/
interface SourceLocation {
/** Source document identifier (file path or URI) */
document: string;
/** Position within the document */
Position: EnhancedPosition;
}
interface EnhancedPosition {
/** Line number (1-based) */
line: number;
/** Column number (1-based) */
column: number;
/** Character offset from start of document (0-based) */
character?: number;
}Event emitter specifically designed for AutoRest message handling.
/**
* Event emitter for AutoRest messages and artifacts
* Provides structured event handling for pipeline communication
*/
class MessageEmitter {
/** Event fired when a file is generated */
GeneratedFile: IEvent<MessageEmitter, Artifact>;
/** Event fired when a file should be protected from deletion */
ProtectFile: IEvent<MessageEmitter, string>;
/** Event fired when a folder should be cleared */
ClearFolder: IEvent<MessageEmitter, string>;
/** Internal data store for pipeline data */
readonly DataStore: DataStore;
/** Cancellation token for operation control */
readonly CancellationToken: CancellationToken;
/** Source for creating cancellation tokens */
readonly CancellationTokenSource: CancellationTokenSource;
}Message Processing Example:
import { AutoRest } from "@autorest/core";
const autorest = new AutoRest((message) => {
// Structured message handling
const timestamp = new Date().toISOString();
const plugin = message.Plugin ? `[${message.Plugin}] ` : '';
const location = message.Source?.[0]
? ` (${message.Source[0].document}:${message.Source[0].Position.line})`
: '';
const formatted = `${timestamp} ${plugin}${message.Text}${location}`;
switch (message.Channel) {
case Channel.Fatal:
case Channel.Error:
console.error(formatted);
if (message.Details) {
console.error('Details:', JSON.stringify(message.Details, null, 2));
}
break;
case Channel.Warning:
console.warn(formatted);
break;
case Channel.Information:
console.log(formatted);
break;
case Channel.File:
console.log(`📁 Generated: ${message.Text}`);
break;
case Channel.Debug:
case Channel.Verbose:
if (process.env.AUTOREST_DEBUG) {
console.debug(formatted);
}
break;
}
});
// Subscribe to artifact events
autorest.GeneratedFile.Subscribe((sender, artifact) => {
console.log(`✅ Generated ${artifact.type}: ${artifact.uri}`);
// Process source maps if available
const artifactMessage = artifact as ArtifactMessage['Details'];
if (artifactMessage.sourceMap) {
console.log(` 📍 Source map available`);
}
});