Pronounced share-roar
Caution
Experimental. Do not use this yet!
A tool to keep error codes in your codebase useful, up‑to‑date, and shareable by leveraging GitHub Discussions.
- Define error codes with rich messages and metadata in TypeScript
- Automatically create and sync GitHub Discussions for each error
- Enrich runtime errors with user-friendly messages and direct discussion links
- Track error locations with automatic code path detection
- Supports styled console output with emoji and colors
- Customizable error printing
deno add @polyseam/sherror- Create a configuration file (e.g.,
sherror.config.ts):
import type { SherrorConfig } from "@polyseam/sherror";
export const config: SherrorConfig = {
category_name: "sherror-errors",
errors: [
{
error_code: 1,
app_message: "<🔴>You must include a <🔵>--foo</🔵> option</🔴>",
post_title: "Error 1: Missing required option",
post_body:
"## Description\nThis error occurs when the required `--foo` option is missing.\n\n## Resolution\nMake sure to include the `--foo` option with a valid value.",
_discussion_link: "https://github.com/your-org/your-repo/discussions/1",
},
{
error_code: 2,
app_message: "<🔴>Invalid value for <🔵>--bar</🔵> option</🔴>",
post_title: "Error 2: Invalid option value",
post_body:
"## Description\nThe value provided for `--bar` is invalid.\n\n## Valid Values\n- Value 1\n- Value 2\n- Value 3",
},
],
printer: (error, codepath) => {
console.debug(
`Error Code: ${error.error_code}\n` +
`App Message: ${error.app_message}\n` +
`Codepath: ${codepath || "N/A"}\n` +
`Discussion Link: ${error._discussion_link || "N/A"}`,
);
},
} satisfies SherrorConfig;- Create a build script (e.g.,
build.ts) to sync errors with GitHub Discussions:
import { SherrorClient } from "@polyseam/sherror";
import { config } from "./sherror.config.ts";
const sc = new SherrorClient(config);
console.log("Synchronizing errors with GitHub Discussions...");
await sc.sync();- Use errors in your application:
import { config } from "./sherror.config.ts";
import { SherrorClient } from "@polyseam/sherror";
// Initialize the Sherror client
const sc = new SherrorClient(config);
// Get an error by code
const error = sc.get(1);
const codepath = new error.Codepath();
// Print the error with codepath
error.print(codepath);
// Optionally, exit the process with the error code
error.exit();To enable GitHub Discussions integration:
- Create a personal access token with
public_reposcope at GitHub Settings > Developer Settings > Personal Access Tokens - Set the token as an environment variable:
export GITHUB_TOKEN=your_token_here - Enable Discussions in your repository settings:
- Go to your repository on GitHub
- Click on "Settings" > "Options"
- Scroll down to "Features"
- Enable "Discussions"
- Create a category (e.g., "sherror-errors") in the Discussions settings
- Set the
category_namein your config to match the category you created - Run your build script to sync errors with GitHub Discussions
class SherrorClient {
constructor(config: SherrorConfig);
// Get an error by its code
get(code: number): SherrorError;
// Sync errors with GitHub Discussions
sync(): Promise<void>;
// Get the current configuration
getConfig(): SherrorConfig;
}interface SherrorConfig {
// Name of the GitHub Discussions category
category_name: string;
// Array of error definitions
errors: SherrorError[];
// Optional custom printer function
printer?: (error: SherrorError, codepath?: string) => void;
}
interface SherrorError {
// Unique error code (number)
error_code: number;
// Message to display to users (supports HTML-like tags for styling)
app_message: string;
// Title for the GitHub Discussion
post_title: string;
// Body content for the GitHub Discussion (supports Markdown)
post_body: string;
// Automatically managed - link to the GitHub Discussion
_discussion_link?: string;
}Sherror supports simple styling in error messages using HTML-like tags:
<🔴>Red text</🔴>- Red text for error messages<🔵>Blue text</🔵>- Blue text for important values
These will be converted to appropriate ANSI escape codes when logged to the console.
You can customize how errors are printed by providing a printer function in
your config:
printer: ((error, codepath) => {
console.error(
`[ERROR ${error.error_code}] ${error.app_message}\n` +
`Location: ${codepath || "unknown"}\n` +
`For more information, see: ${
error._discussion_link || "No discussion available"
}`,
);
});Contributions are welcome! Please open an issue or submit a pull request.
- Clone the repository
- Run tests:
deno test - Make your changes
- Ensure tests pass
- Submit a pull request
MIT