Files
factorio-clusterio/packages/lib/src/errors.ts
T
Hornwitser 276c36b6e9 Throw error if lib is imported twice (#863)
The lib library was written with the assumption that there's only one
copy of it in the whole application and everything imports that one
copy.  Unfortunately there are quite a few circumstances were package
managers end up installing multiple copies of lib and then different
parts use different versions of the library.

This causes Clusterio to break with strange and hard to track down
errors.  Most notably permissions and link messages for a plugin may get
registered to one copy of lib while the controller uses a different copy
and thus does not see or register that what the plugin added exists.

Detect this condition when importing @clusterio/lib and throw an error
when it has been imported twice in order to prevent the broken
installation from causing future hard to debug issues.
2026-03-06 18:50:18 +01:00

125 lines
2.3 KiB
TypeScript

/**
* Errors thrown in Clusterio
* @module lib/errors
*/
/**
* Thrown by commands when they fail
*/
export class CommandError extends Error {
code = "CommandError";
}
/**
* Thrown if the installation is broken, such as when multiple copies of lib is imported
*/
export class InstallationError extends Error {
code = "InstallationError";
}
/**
* Thrown from requests sent when an error occured handling it
*/
export class RequestError extends Error {
constructor(
message: string,
public code = "RequestError",
public stack?: string,
) {
super(message);
}
}
/**
* Thrown when a permission check fails.
*
* Is a subclass of RequestError to prevent logging stack traces when
* requests fail due to permission denied.
*
*/
export class PermissionError extends RequestError {
constructor(
message: string,
code = "PermissionError",
stack?: string,
) {
super(message, code, stack);
}
}
/**
* Signal for messages that fail validation
*/
export class InvalidMessage extends Error {
code = "InvalidMessage";
constructor(
message: string,
public errors?: object | null,
) {
super(message);
}
}
/**
* Thrown from requests when the session it was sent on was lost
*/
export class SessionLost extends Error {
code = "SessionLost";
}
/**
* Super class for all custom websocket errors
*/
export class WebSocketError extends Error {
code = "WebSocketError";
}
/**
* Thrown when WebSocket authentication failed
*/
export class AuthenticationFailed extends WebSocketError {
code = "AuthenticationFailed";
}
/**
* Thrown when WebSocket protocol is violated
*/
export class ProtocolError extends WebSocketError {
code = "ProtocolError";
}
/**
* Thrown when WebSocket policy is violated
*/
export class PolicyViolation extends WebSocketError {
code = "PolicyViolation";
}
/**
* Errror class for known errors occuring during startup
*/
export class StartupError extends Error {
code = "StartupError";
}
/**
* Errors outside of our control
*/
export class EnvironmentError extends Error {
code = "EnvironmentError";
}
/**
* Errors caused by plugins
*/
export class PluginError extends Error {
code = "PluginError";
constructor(
public pluginName: string,
public original: Error
) {
super(`PluginError: ${original.message}`);
}
}