mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 08:13:12 +09:00
Keep error classes across links
Error responses now carry the registered name of the thrown error's class, and the requester rejects with an instance of that class instead of a generic RequestError. Error classes are registered like message classes, and plugins list their own in `errors`. RequestError is replaced by a hierarchy following the HTTP status codes: ClientError (ValidationError, ForbiddenError, NotFoundError, TimeoutError, ConflictError) and ServerError (InternalServerError). PermissionError is PermissionDeniedError and the config errors InvalidValue, InvalidField and InvalidAccess gain an Error suffix. Link logs only errors whose class is not registered. These arrive on the other side as InternalServerError with the original message and stack, so an error forwarded through the controller is logged once, where it was thrown. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ffe82f81d8
commit
1c7c00b033
37 files changed
+589
-310
No files matched your search
@@ -63,7 +63,7 @@ export default class BaseConnection extends lib.Link {
|
||||
let { id } = request;
|
||||
let modPack = this._controller.modPacks.get(id);
|
||||
if (!modPack) {
|
||||
throw new lib.RequestError(`Mod pack with ID ${id} does not exist`);
|
||||
throw new lib.NotFoundError(`Mod pack with ID ${id} does not exist`);
|
||||
}
|
||||
return modPack;
|
||||
}
|
||||
@@ -71,11 +71,11 @@ export default class BaseConnection extends lib.Link {
|
||||
async handleModPackGetDefaultRequest(): Promise<lib.ModPack> {
|
||||
let id = this._controller.config.get("controller.default_mod_pack_id");
|
||||
if (id === null) {
|
||||
throw new lib.RequestError("Default mod pack not set on controller");
|
||||
throw new lib.ConflictError("Default mod pack not set on controller");
|
||||
}
|
||||
let modPack = this._controller.modPacks.get(id);
|
||||
if (!modPack) {
|
||||
throw new lib.RequestError(`Default mod pack configured (${id}) does not exist`);
|
||||
throw new lib.NotFoundError(`Default mod pack configured (${id}) does not exist`);
|
||||
}
|
||||
return modPack;
|
||||
}
|
||||
@@ -84,10 +84,10 @@ export default class BaseConnection extends lib.Link {
|
||||
let filename = lib.ModInfo.filename(mod.name, mod.version);
|
||||
let modInfo = this._controller.modStore.files.get(filename);
|
||||
if (!modInfo) {
|
||||
throw new lib.RequestError(`Mod ${filename} does not exist on controller`);
|
||||
throw new lib.NotFoundError(`Mod ${filename} does not exist on controller`);
|
||||
}
|
||||
if (mod.sha1 && mod.sha1 !== modInfo.sha1) {
|
||||
throw new lib.RequestError(`Mod ${filename} checksum does not match controller's checksum`);
|
||||
throw new lib.ConflictError(`Mod ${filename} checksum does not match controller's checksum`);
|
||||
}
|
||||
return modInfo;
|
||||
}
|
||||
|
||||
@@ -155,7 +155,7 @@ export default class ControlConnection extends BaseConnection {
|
||||
try {
|
||||
this.checkPermission(message, entry);
|
||||
} catch (err: any) {
|
||||
this.connector.sendResponseError(new lib.ResponseError(err.message, err.code), message.src);
|
||||
this.connector.sendResponseError(lib.Link.errorToResponse(err, false), message.src);
|
||||
logger.audit(`Permission denied for ${message.name} by ${this.user.id} from ${this.connector.dst}`);
|
||||
throw err;
|
||||
}
|
||||
@@ -197,7 +197,7 @@ export default class ControlConnection extends BaseConnection {
|
||||
|
||||
async handleControllerRestartRequest() {
|
||||
if (!this._controller.canRestart) {
|
||||
throw new lib.RequestError("Cannot restart, controller does not have a process monitor to restart it.");
|
||||
throw new lib.ConflictError("Cannot restart, controller does not have a process monitor to restart it.");
|
||||
}
|
||||
if (await lib.packageOperationRunning()) {
|
||||
throw new lib.RequestError("Cannot restart while a plugin install or update is running on this machine.");
|
||||
@@ -205,7 +205,7 @@ export default class ControlConnection extends BaseConnection {
|
||||
const downgrade = await this._controller.checkRestartDowngrade();
|
||||
if (downgrade) {
|
||||
const { installedVersion, runningVersion } = downgrade;
|
||||
throw new lib.RequestError(
|
||||
throw new lib.ConflictError(
|
||||
`Cannot restart controller with installed Clusterio version ${installedVersion} because it ` +
|
||||
`is older than running version ${runningVersion}. ` +
|
||||
"Stop the controller before starting the older version manually."
|
||||
@@ -303,13 +303,13 @@ export default class ControlConnection extends BaseConnection {
|
||||
|
||||
async handleInstanceCreateRequest(request: lib.InstanceCreateRequest) {
|
||||
if ((request.config["instance.assigned_host"] ?? null) !== null) {
|
||||
throw new lib.RequestError("instance.assigned_host must be set through the assign-host interface");
|
||||
throw new lib.ValidationError("instance.assigned_host must be set through the assign-host interface");
|
||||
}
|
||||
const instanceConfig = new lib.InstanceConfig("controller");
|
||||
if (request.cloneFromId) {
|
||||
const baseInstance = this._controller.instances.get(request.cloneFromId);
|
||||
if (!baseInstance) {
|
||||
throw new lib.RequestError(`Instance with ID ${request.cloneFromId} does not exist`);
|
||||
throw new lib.NotFoundError(`Instance with ID ${request.cloneFromId} does not exist`);
|
||||
}
|
||||
instanceConfig.update(baseInstance.config.toJSON(), false);
|
||||
instanceConfig.set("instance.assigned_host", null); // New instances are unassigned
|
||||
@@ -330,12 +330,12 @@ export default class ControlConnection extends BaseConnection {
|
||||
|
||||
private static validateInstanceConfigSetField(fieldName: string) {
|
||||
if (fieldName === "instance.assigned_host") {
|
||||
throw new lib.RequestError("instance.assigned_host must be set through the assign-host interface");
|
||||
throw new lib.ValidationError("instance.assigned_host must be set through the assign-host interface");
|
||||
}
|
||||
|
||||
if (fieldName === "instance.id") {
|
||||
// XXX is this worth implementing? It's race condition galore.
|
||||
throw new lib.RequestError("Setting instance.id is not supported");
|
||||
throw new lib.ValidationError("Setting instance.id is not supported");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -393,7 +393,7 @@ export default class ControlConnection extends BaseConnection {
|
||||
let ready = new Promise<void>((resolve, reject) => {
|
||||
stream.events.on("source", resolve);
|
||||
stream.events.on("timeout", () => reject(
|
||||
new lib.RequestError("Timed out establishing stream from host")
|
||||
new lib.TimeoutError("Timed out establishing stream from host")
|
||||
));
|
||||
});
|
||||
ready.catch(() => {});
|
||||
@@ -410,17 +410,17 @@ export default class ControlConnection extends BaseConnection {
|
||||
|
||||
async handleInstanceTransferSaveRequest(request: lib.InstanceTransferSaveRequest) {
|
||||
if (request.sourceInstanceId === request.targetInstanceId) {
|
||||
throw new lib.RequestError("Source and target instance may not be the same");
|
||||
throw new lib.ValidationError("Source and target instance may not be the same");
|
||||
}
|
||||
let sourceInstance = this._controller.instances.getForRequest(request.sourceInstanceId);
|
||||
let targetInstance = this._controller.instances.getForRequest(request.targetInstanceId);
|
||||
let sourceHostId = sourceInstance.config.get("instance.assigned_host");
|
||||
let targetHostId = targetInstance.config.get("instance.assigned_host");
|
||||
if (sourceHostId === null) {
|
||||
throw new lib.RequestError("Source instance is not assigned a host");
|
||||
throw new lib.ConflictError("Source instance is not assigned a host");
|
||||
}
|
||||
if (targetHostId === null) {
|
||||
throw new lib.RequestError("Target instance is not assigned a host");
|
||||
throw new lib.ConflictError("Target instance is not assigned a host");
|
||||
}
|
||||
|
||||
// Let host handle request if source and target is on the same host.
|
||||
@@ -431,12 +431,12 @@ export default class ControlConnection extends BaseConnection {
|
||||
// Check connectivity
|
||||
let sourceHostConnection = this._controller.wsServer.hostConnections.get(sourceHostId);
|
||||
if (!sourceHostConnection || sourceHostConnection.connector.closing) {
|
||||
throw new lib.RequestError("Source host is not connected to the controller");
|
||||
throw new lib.ConflictError("Source host is not connected to the controller");
|
||||
}
|
||||
|
||||
let targetHostConnection = this._controller.wsServer.hostConnections.get(targetHostId);
|
||||
if (!targetHostConnection || targetHostConnection.connector.closing) {
|
||||
throw new lib.RequestError("Target host is not connected to the controller");
|
||||
throw new lib.ConflictError("Target host is not connected to the controller");
|
||||
}
|
||||
|
||||
// Create stream to proxy from target to source
|
||||
@@ -445,7 +445,7 @@ export default class ControlConnection extends BaseConnection {
|
||||
if (stream.source) {
|
||||
stream.source.destroy();
|
||||
}
|
||||
stream.events.emit("error", new lib.RequestError("Timed out establishing transfer stream"));
|
||||
stream.events.emit("error", new lib.TimeoutError("Timed out establishing transfer stream"));
|
||||
});
|
||||
|
||||
// Ignore errors if not listening for them to avoid crash.
|
||||
@@ -485,10 +485,10 @@ export default class ControlConnection extends BaseConnection {
|
||||
async handleModPackCreateRequest(request: lib.ModPackCreateRequest) {
|
||||
let modPack = request.modPack;
|
||||
if (modPack.id === undefined) {
|
||||
throw new lib.RequestError("Mod pack need an ID to be created");
|
||||
throw new lib.ValidationError("Mod pack need an ID to be created");
|
||||
}
|
||||
if (this._controller.modPacks.has(modPack.id)) {
|
||||
throw new lib.RequestError(`Mod pack with ID ${modPack.id} already exist`);
|
||||
throw new lib.ConflictError(`Mod pack with ID ${modPack.id} already exist`);
|
||||
}
|
||||
this._controller.modPacks.set(modPack);
|
||||
}
|
||||
@@ -496,7 +496,7 @@ export default class ControlConnection extends BaseConnection {
|
||||
async handleModPackUpdateRequest(request: lib.ModPackUpdateRequest) {
|
||||
let modPack = request.modPack;
|
||||
if (modPack.id === undefined || !this._controller.modPacks.has(modPack.id)) {
|
||||
throw new lib.RequestError(`Mod pack with ID ${modPack.id} does not exist`);
|
||||
throw new lib.NotFoundError(`Mod pack with ID ${modPack.id} does not exist`);
|
||||
}
|
||||
this._controller.modPacks.set(modPack);
|
||||
}
|
||||
@@ -505,7 +505,7 @@ export default class ControlConnection extends BaseConnection {
|
||||
let { id } = request;
|
||||
let modPack = this._controller.modPacks.getMutable(id);
|
||||
if (!modPack) {
|
||||
throw new lib.RequestError(`Mod pack with ID ${id} does not exist`);
|
||||
throw new lib.NotFoundError(`Mod pack with ID ${id} does not exist`);
|
||||
}
|
||||
this._controller.modPacks.delete(modPack);
|
||||
this._controller.deleteNote("mod_pack", id);
|
||||
@@ -587,7 +587,7 @@ export default class ControlConnection extends BaseConnection {
|
||||
author: (a:ModVersions, b:ModVersions) => strcmp(a.versions[0].author, b.versions[0].author),
|
||||
};
|
||||
if (!Object.prototype.hasOwnProperty.call(sorters, sort)) {
|
||||
throw new lib.RequestError(`Invalid value for sort: ${sort}`);
|
||||
throw new lib.ValidationError(`Invalid value for sort: ${sort}`);
|
||||
}
|
||||
resultList.sort(sorters[sort as keyof typeof sorters]);
|
||||
let order = request.sortOrder;
|
||||
@@ -634,7 +634,7 @@ export default class ControlConnection extends BaseConnection {
|
||||
} catch (error: any) {
|
||||
logger.error(`Error fetching all mods from portal (${request.factorioVersion}): ${error}`);
|
||||
// Propagate a user-friendly error back to the client
|
||||
throw new lib.RequestError(`Portal mod fetch failed: ${error}`);
|
||||
throw new lib.InternalServerError(`Portal mod fetch failed: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -652,7 +652,7 @@ export default class ControlConnection extends BaseConnection {
|
||||
const token = this._controller.config.get("controller.factorio_token");
|
||||
|
||||
if (!username || username === "" || !token || token === "") {
|
||||
throw new lib.RequestError("Factorio credentials (username, token) not configured on the controller.");
|
||||
throw new lib.ConflictError("Factorio credentials (username, token) not configured on the controller.");
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -661,18 +661,16 @@ export default class ControlConnection extends BaseConnection {
|
||||
|
||||
} catch (error: any) {
|
||||
logger.error(`Error downloading mods from portal: ${error.message}`);
|
||||
// Improve error message clarity
|
||||
let errorMessage = "Mod portal download failed";
|
||||
if (error instanceof lib.RequestError) {
|
||||
errorMessage += `: ${error.message}`;
|
||||
} else if (error.message.includes("401") || error.message.includes("Unauthorized")) {
|
||||
errorMessage += ": Authentication failed. Check Factorio username/token in controller config.";
|
||||
} else if (error.message.includes("404") || error.message.includes("Not Found")) {
|
||||
errorMessage += ": Mod version not found on the portal.";
|
||||
} else {
|
||||
errorMessage += `: ${error.message}`;
|
||||
const errorMessage = "Mod portal download failed";
|
||||
if (error.message.includes("401") || error.message.includes("Unauthorized")) {
|
||||
throw new lib.ConflictError(
|
||||
`${errorMessage}: Authentication failed. Check Factorio username/token in controller config.`
|
||||
);
|
||||
}
|
||||
throw new lib.RequestError(errorMessage);
|
||||
if (error.message.includes("404") || error.message.includes("Not Found")) {
|
||||
throw new lib.NotFoundError(`${errorMessage}: Mod version not found on the portal.`);
|
||||
}
|
||||
throw new lib.InternalServerError(`${errorMessage}: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -682,7 +680,7 @@ export default class ControlConnection extends BaseConnection {
|
||||
for (const mod of request.mods) {
|
||||
const modInfo = installedMods.find(m => m.name === mod.name && mod.version.testVersion(m.version));
|
||||
if (!modInfo) {
|
||||
throw new lib.RequestError(`Failed to find mod ${mod.name} in store after download attempt.`);
|
||||
throw new lib.InternalServerError(`Failed to find mod ${mod.name} in store after download attempt.`);
|
||||
}
|
||||
modInfos.push(modInfo);
|
||||
}
|
||||
@@ -716,7 +714,7 @@ export default class ControlConnection extends BaseConnection {
|
||||
} catch (error: any) {
|
||||
if (!error.message.includes("404") && !error.message.includes("Not Found")) {
|
||||
logger.error(`Error fetching mod from portal: ${error.message}`);
|
||||
throw new lib.RequestError(`Dependency resolution failed: ${error.message}`);
|
||||
throw new lib.InternalServerError(`Dependency resolution failed: ${error.message}`);
|
||||
} else {
|
||||
releases = null;
|
||||
}
|
||||
@@ -956,7 +954,7 @@ export default class ControlConnection extends BaseConnection {
|
||||
let { id, name, description, permissions } = request;
|
||||
let role = this._controller.roles.getMutable(id);
|
||||
if (!role) {
|
||||
throw new lib.RequestError(`Role with ID ${id} does not exist`);
|
||||
throw new lib.NotFoundError(`Role with ID ${id} does not exist`);
|
||||
}
|
||||
|
||||
role.name = name;
|
||||
@@ -968,7 +966,7 @@ export default class ControlConnection extends BaseConnection {
|
||||
async handleRoleGrantDefaultPermissionsRequest(request: lib.RoleGrantDefaultPermissionsRequest) {
|
||||
let role = this._controller.roles.get(request.id);
|
||||
if (!role) {
|
||||
throw new lib.RequestError(`Role with ID ${request.id} does not exist`);
|
||||
throw new lib.NotFoundError(`Role with ID ${request.id} does not exist`);
|
||||
}
|
||||
|
||||
role.grantDefaultPermissions();
|
||||
@@ -980,7 +978,7 @@ export default class ControlConnection extends BaseConnection {
|
||||
|
||||
const role = this._controller.roles.get(id);
|
||||
if (!role) {
|
||||
throw new lib.RequestError(`Role with ID ${id} does not exist`);
|
||||
throw new lib.NotFoundError(`Role with ID ${id} does not exist`);
|
||||
}
|
||||
this._controller.roles.delete(role);
|
||||
this._controller.deleteNote("role", id);
|
||||
@@ -1008,7 +1006,7 @@ export default class ControlConnection extends BaseConnection {
|
||||
let name = request.name;
|
||||
let user = this._controller.users.getByName(name);
|
||||
if (!user) {
|
||||
throw new lib.RequestError(`User ${name} does not exist`);
|
||||
throw new lib.NotFoundError(`User ${name} does not exist`);
|
||||
}
|
||||
|
||||
return user.toUserDetails();
|
||||
@@ -1025,7 +1023,7 @@ export default class ControlConnection extends BaseConnection {
|
||||
async handleUserRevokeTokenRequest(request: lib.UserRevokeTokenRequest) {
|
||||
let user = this._controller.users.getByName(request.name);
|
||||
if (!user) {
|
||||
throw new lib.RequestError(`User '${request.name}' does not exist`);
|
||||
throw new lib.NotFoundError(`User '${request.name}' does not exist`);
|
||||
}
|
||||
if (user.id !== this.user.id) {
|
||||
this.user.checkPermission("core.user.revoke_other_token");
|
||||
@@ -1042,12 +1040,12 @@ export default class ControlConnection extends BaseConnection {
|
||||
async handleUserUpdateRolesRequest(request: lib.UserUpdateRolesRequest) {
|
||||
let user = this._controller.users.getByName(request.name);
|
||||
if (!user) {
|
||||
throw new lib.RequestError(`User '${request.name}' does not exist`);
|
||||
throw new lib.NotFoundError(`User '${request.name}' does not exist`);
|
||||
}
|
||||
|
||||
for (let roleId of request.roles) {
|
||||
if (!this._controller.roles.has(roleId)) {
|
||||
throw new lib.RequestError(`Role with ID ${roleId} does not exist`);
|
||||
throw new lib.NotFoundError(`Role with ID ${roleId} does not exist`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1063,7 +1061,7 @@ export default class ControlConnection extends BaseConnection {
|
||||
this.user.checkPermission("core.user.create");
|
||||
user = this._controller.users.createUser(name);
|
||||
} else {
|
||||
throw new lib.RequestError(`User '${name}' does not exist`);
|
||||
throw new lib.NotFoundError(`User '${name}' does not exist`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1079,7 +1077,7 @@ export default class ControlConnection extends BaseConnection {
|
||||
this.user.checkPermission("core.user.create");
|
||||
user = this._controller.users.createUser(name);
|
||||
} else {
|
||||
throw new lib.RequestError(`User '${name}' does not exist`);
|
||||
throw new lib.NotFoundError(`User '${name}' does not exist`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1097,7 +1095,7 @@ export default class ControlConnection extends BaseConnection {
|
||||
this.user.checkPermission("core.user.create");
|
||||
user = this._controller.users.createUser(name);
|
||||
} else {
|
||||
throw new lib.RequestError(`User '${name}' does not exist`);
|
||||
throw new lib.NotFoundError(`User '${name}' does not exist`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1109,7 +1107,7 @@ export default class ControlConnection extends BaseConnection {
|
||||
let name = request.name;
|
||||
let user = this._controller.users.getByName(name);
|
||||
if (!user) {
|
||||
throw new lib.RequestError(`User '${name}' does not exist`);
|
||||
throw new lib.NotFoundError(`User '${name}' does not exist`);
|
||||
}
|
||||
|
||||
this._controller.users.deleteUser(user);
|
||||
@@ -1340,21 +1338,21 @@ export default class ControlConnection extends BaseConnection {
|
||||
|
||||
async handleControllerUpdateRequest(request: lib.ControllerUpdateRequest) {
|
||||
if (!this._controller.config.get("controller.allow_remote_updates")) {
|
||||
throw new lib.RequestError("Remote updates are disabled on this machine");
|
||||
throw new lib.ForbiddenError("Remote updates are disabled on this machine");
|
||||
}
|
||||
return await lib.updatePackage("@clusterio/controller");
|
||||
}
|
||||
|
||||
async handlePluginUpdateRequest(request: lib.PluginUpdateRequest) {
|
||||
if (!this._controller.config.get("controller.allow_plugin_updates")) {
|
||||
throw new lib.RequestError("Plugin updates are disabled on this machine");
|
||||
throw new lib.ForbiddenError("Plugin updates are disabled on this machine");
|
||||
}
|
||||
return await lib.handlePluginUpdate(request.pluginPackage, this._controller.pluginInfos);
|
||||
}
|
||||
|
||||
async handlePluginInstallRequest(request: lib.PluginInstallRequest) {
|
||||
if (!this._controller.config.get("controller.allow_plugin_install")) {
|
||||
throw new lib.RequestError("Plugin installs are disabled on this machine");
|
||||
throw new lib.ForbiddenError("Plugin installs are disabled on this machine");
|
||||
}
|
||||
const npmPackage = await lib.handlePluginInstall(request.pluginPackage);
|
||||
// A new plugin is not in pluginInfos, so checkRestartRequired cannot notice it. Version
|
||||
@@ -1366,10 +1364,10 @@ export default class ControlConnection extends BaseConnection {
|
||||
|
||||
async handleUpdateAllRequest(request: lib.UpdateAllRequest) {
|
||||
if (!this._controller.config.get("controller.allow_remote_updates")) {
|
||||
throw new lib.RequestError("Remote updates are disabled on this machine");
|
||||
throw new lib.ForbiddenError("Remote updates are disabled on this machine");
|
||||
}
|
||||
if (!this._controller.config.get("controller.allow_plugin_updates")) {
|
||||
throw new lib.RequestError("Plugin updates are disabled on this machine");
|
||||
throw new lib.ForbiddenError("Plugin updates are disabled on this machine");
|
||||
}
|
||||
return await lib.handleUpdateAll("@clusterio/controller", this._controller.pluginInfos);
|
||||
}
|
||||
|
||||
@@ -1465,24 +1465,24 @@ export default class Controller {
|
||||
} else if (dst.type === lib.Address.control) {
|
||||
connection = this.wsServer.controlConnections.get(dst.id);
|
||||
if (!connection) {
|
||||
throw new lib.RequestError("Target control connection does not exist.");
|
||||
throw new lib.NotFoundError("Target control connection does not exist.");
|
||||
}
|
||||
|
||||
} else if (dst.type === lib.Address.instance) {
|
||||
const instance = this.instances.getForRequest(dst.id);
|
||||
let hostId = instance.config.get("instance.assigned_host");
|
||||
if (hostId === null) {
|
||||
throw new lib.RequestError("Instance is not assigned to a host");
|
||||
throw new lib.ConflictError("Instance is not assigned to a host");
|
||||
}
|
||||
connection = this.wsServer.hostConnections.get(hostId);
|
||||
if (!connection) {
|
||||
throw new lib.RequestError("Host containing instance is not connected");
|
||||
throw new lib.ConflictError("Host containing instance is not connected");
|
||||
}
|
||||
|
||||
} else if (dst.type === lib.Address.host) {
|
||||
connection = this.wsServer.hostConnections.get(dst.id);
|
||||
if (!connection) {
|
||||
throw new lib.RequestError("Host is not connected");
|
||||
throw new lib.ConflictError("Host is not connected");
|
||||
}
|
||||
|
||||
} else {
|
||||
@@ -1566,11 +1566,11 @@ export default class Controller {
|
||||
let instance = this.instances.getForRequest(request.instanceId);
|
||||
let hostId = instance.config.get("instance.assigned_host");
|
||||
if (hostId === null) {
|
||||
throw new lib.RequestError("Instance is not assigned to a host");
|
||||
throw new lib.ConflictError("Instance is not assigned to a host");
|
||||
}
|
||||
let connection = this.wsServer.hostConnections.get(hostId);
|
||||
if (!connection) {
|
||||
throw new lib.RequestError("Host containing instance is not connected");
|
||||
throw new lib.ConflictError("Host containing instance is not connected");
|
||||
}
|
||||
return await connection.send(request);
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ export default class ControllerRouter {
|
||||
return false;
|
||||
}
|
||||
origin.connector.sendResponseError(
|
||||
new lib.ResponseError(msg || "Unroutable destination"), message.src
|
||||
new lib.ResponseError(msg || "Unroutable destination", "ConflictError"), message.src
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -65,12 +65,12 @@ export default class InstanceManager {
|
||||
*
|
||||
* @param instanceId - ID of instance to get.
|
||||
* @returns The instance record.
|
||||
* @throws {module:lib.RequestError} if the instance does not exist.
|
||||
* @throws {module:lib.NotFoundError} if the instance does not exist.
|
||||
*/
|
||||
getForRequest(instanceId: number): InstanceRecord {
|
||||
const instance = this.records.get(instanceId);
|
||||
if (!instance) {
|
||||
throw new lib.RequestError(`Instance with ID ${instanceId} does not exist`);
|
||||
throw new lib.NotFoundError(`Instance with ID ${instanceId} does not exist`);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
@@ -125,7 +125,7 @@ export default class InstanceManager {
|
||||
* @param instanceConfig - Config to base the newly created instance on.
|
||||
* @param _suppressChanges - When true, side effects are suppressed, caller takes ownership.
|
||||
* @returns The created instance record.
|
||||
* @throws {module:lib.RequestError} if an instance with the same ID already exists.
|
||||
* @throws {module:lib.ConflictError} if an instance with the same ID already exists.
|
||||
*/
|
||||
async createInstance(
|
||||
instanceConfig: lib.InstanceConfig,
|
||||
@@ -133,7 +133,7 @@ export default class InstanceManager {
|
||||
): Promise<InstanceRecord> {
|
||||
const instanceId = instanceConfig.get("instance.id");
|
||||
if (this.records.has(instanceId)) {
|
||||
throw new lib.RequestError(`Instance with ID ${instanceId} already exists`);
|
||||
throw new lib.ConflictError(`Instance with ID ${instanceId} already exists`);
|
||||
}
|
||||
|
||||
const controllerName = this._controller.config.get("controller.name");
|
||||
@@ -172,8 +172,8 @@ export default class InstanceManager {
|
||||
* @param hostId - ID of host to assign the instance to. If undefined,
|
||||
* the instance will be unassigned.
|
||||
*
|
||||
* @throws {module:lib.RequestError} if the instance does not exist.
|
||||
* @throws {module:lib.RequestError} if the target host is not connected.
|
||||
* @throws {module:lib.NotFoundError} if the instance does not exist.
|
||||
* @throws {module:lib.ConflictError} if the target host is not connected.
|
||||
*/
|
||||
async assignInstance(
|
||||
instanceId: number,
|
||||
@@ -191,7 +191,7 @@ export default class InstanceManager {
|
||||
if (hostId !== null) {
|
||||
newHostConnection = hostConnections.get(hostId);
|
||||
if (!newHostConnection) {
|
||||
throw new lib.RequestError("Target host is not connected to the controller");
|
||||
throw new lib.ConflictError("Target host is not connected to the controller");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -247,7 +247,7 @@ export default class InstanceManager {
|
||||
*
|
||||
* @param instanceId - ID of instance to delete.
|
||||
*
|
||||
* @throws {module:lib.RequestError} if the instance does not exist.
|
||||
* @throws {module:lib.NotFoundError} if the instance does not exist.
|
||||
*/
|
||||
async deleteInstance(instanceId: number): Promise<void> {
|
||||
const instance = this.getForRequest(instanceId);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
|
||||
import {
|
||||
IUser, Role, SubscribableDatastore,
|
||||
PermissionError, PermissionName, permissions,
|
||||
PermissionDeniedError, PermissionName, permissions,
|
||||
} from "@clusterio/lib";
|
||||
|
||||
import UserRecord from "./UserRecord.js";
|
||||
@@ -141,6 +141,6 @@ export default class User extends UserRecord implements IUser {
|
||||
}
|
||||
}
|
||||
|
||||
throw new PermissionError("Permission denied");
|
||||
throw new PermissionDeniedError("Permission denied");
|
||||
}
|
||||
}
|
||||
@@ -548,7 +548,7 @@ function checkModName(name: string) {
|
||||
try {
|
||||
lib.checkFilename(name);
|
||||
} catch (err: any) {
|
||||
throw new lib.RequestError(`Mod name ${err.message}`);
|
||||
throw new lib.ValidationError(`Mod name ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+6
-6
@@ -307,12 +307,12 @@ async function startCtl() {
|
||||
logger.error(`Error running command: ${err.message}`);
|
||||
process.exitCode = 1;
|
||||
|
||||
} else if (err instanceof lib.RequestError) {
|
||||
if (err.stack) {
|
||||
logger.error(`Error sending request:\n${err.stack}`);
|
||||
} else {
|
||||
logger.error(`Error sending request: ${err.message}`);
|
||||
}
|
||||
} else if (err instanceof lib.ClientError) {
|
||||
logger.error(`Error sending request: ${err.message}`);
|
||||
process.exitCode = 1;
|
||||
|
||||
} else if (err instanceof lib.ServerError) {
|
||||
logger.error(`Error sending request:\n${err.stack ?? err.message}`);
|
||||
process.exitCode = 1;
|
||||
|
||||
} else {
|
||||
|
||||
+26
-30
@@ -22,7 +22,7 @@ function checkRequestSaveName(name: string) {
|
||||
try {
|
||||
lib.checkFilename(name);
|
||||
} catch (err: any) {
|
||||
throw new lib.RequestError(`Save name ${err.message}`);
|
||||
throw new lib.ValidationError(`Save name ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -203,7 +203,7 @@ export class HostRouter {
|
||||
return false;
|
||||
}
|
||||
origin.connector.sendResponseError(
|
||||
new lib.ResponseError(msg || "Instance is not running."), message.src
|
||||
new lib.ResponseError(msg || "Instance is not running.", "ConflictError"), message.src
|
||||
);
|
||||
}
|
||||
return true;
|
||||
@@ -265,13 +265,13 @@ export class HostRouter {
|
||||
let dst = message.dst;
|
||||
if (nextHop) {
|
||||
origin.connector.sendResponseError(
|
||||
new lib.ResponseError("Instance is already running."), message.src
|
||||
new lib.ResponseError("Instance is already running.", "ConflictError"), message.src
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (this.host._shuttingDown) {
|
||||
origin.connector.sendResponseError(
|
||||
new lib.ResponseError("Host is shutting down."), message.src
|
||||
new lib.ResponseError("Host is shutting down.", "ConflictError"), message.src
|
||||
);
|
||||
return;
|
||||
}
|
||||
@@ -280,9 +280,7 @@ export class HostRouter {
|
||||
instanceConnection.connector.forward(message);
|
||||
}).catch(err => {
|
||||
logger.error(`Error starting instance:\n${err.stack ?? err.message}`, this.host.instanceLogMeta(dst.id));
|
||||
origin.connector.sendResponseError(
|
||||
new lib.ResponseError(err.message, err.code, err.stack), message.src
|
||||
);
|
||||
origin.connector.sendResponseError(lib.Link.errorToResponse(err), message.src);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -295,9 +293,7 @@ export class HostRouter {
|
||||
}
|
||||
} catch (err: any) {
|
||||
if (message.type === "request") {
|
||||
origin.connector.sendResponseError(
|
||||
new lib.ResponseError(err.message, err.code, err.stack), message.src
|
||||
);
|
||||
origin.connector.sendResponseError(lib.Link.errorToResponse(err), message.src);
|
||||
}
|
||||
logger.warn(`Failed to deliver ${(message as any).name || "message"} (${message.type}): ${err.message}`);
|
||||
if (err instanceof lib.InvalidMessage && err.errors) {
|
||||
@@ -566,15 +562,15 @@ export default class Host extends lib.Link {
|
||||
|
||||
async handleHostRestartRequest() {
|
||||
if (!this.canRestart) {
|
||||
throw new lib.RequestError("Cannot restart, host does not have a process monitor to restart it.");
|
||||
throw new lib.ConflictError("Cannot restart, host does not have a process monitor to restart it.");
|
||||
}
|
||||
if (await lib.packageOperationRunning()) {
|
||||
throw new lib.RequestError("Cannot restart while a plugin install or update is running on this machine.");
|
||||
throw new lib.ConflictError("Cannot restart while a plugin install or update is running on this machine.");
|
||||
}
|
||||
const downgrade = await this.checkRestartDowngrade();
|
||||
if (downgrade) {
|
||||
const { installedVersion, runningVersion } = downgrade;
|
||||
throw new lib.RequestError(
|
||||
throw new lib.ConflictError(
|
||||
`Cannot restart host with installed Clusterio version ${installedVersion} because it is older than ` +
|
||||
`running version ${runningVersion}. Stop the host before starting the older version manually.`
|
||||
);
|
||||
@@ -595,7 +591,7 @@ export default class Host extends lib.Link {
|
||||
if (fieldName === "host.id") {
|
||||
// Changing host.id at runtime is not worth the effort to
|
||||
// support and will break a lot of things if it is allowed.
|
||||
throw new lib.RequestError("Setting 'host.id' while host is running is not supported");
|
||||
throw new lib.ConflictError("Setting 'host.id' while host is running is not supported");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -840,7 +836,7 @@ export default class Host extends lib.Link {
|
||||
getRequestInstance(instanceId: number) {
|
||||
let instance = this.assignedInstances.get(instanceId);
|
||||
if (!instance) {
|
||||
throw new lib.RequestError(`Instance with ID ${instanceId} does not exist`);
|
||||
throw new lib.NotFoundError(`Instance with ID ${instanceId} does not exist`);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
@@ -886,7 +882,7 @@ export default class Host extends lib.Link {
|
||||
}
|
||||
if (!availablePorts.size) {
|
||||
const others = inUse.size ? ` (${inUse.size} in use by other processes)` : "";
|
||||
throw new lib.RequestError(`No available port left to assign from host.factorio_port_range${others}`);
|
||||
throw new lib.ConflictError(`No available port left to assign from host.factorio_port_range${others}`);
|
||||
}
|
||||
const [newPort] = availablePorts;
|
||||
instanceToAssign.config.set("factorio.host_assigned_game_port", newPort);
|
||||
@@ -929,7 +925,7 @@ export default class Host extends lib.Link {
|
||||
async _connectInstance(instanceId: number) {
|
||||
let unloadedInstance = this.getRequestInstance(instanceId);
|
||||
if (this.instanceConnections.has(instanceId) || this._startingInstances.has(instanceId)) {
|
||||
throw new lib.RequestError(`Instance with ID ${instanceId} is running`);
|
||||
throw new lib.ConflictError(`Instance with ID ${instanceId} is running`);
|
||||
}
|
||||
|
||||
let hostAddress = new lib.Address(lib.Address.host, this.config.get("host.id"));
|
||||
@@ -1084,10 +1080,10 @@ export default class Host extends lib.Link {
|
||||
);
|
||||
} catch (err: any) {
|
||||
if (err.code === "EEXIST") {
|
||||
throw new lib.RequestError(`${newName} already exists`);
|
||||
throw new lib.ConflictError(`${newName} already exists`);
|
||||
}
|
||||
if (err.code === "ENOENT") {
|
||||
throw new lib.RequestError(`${oldName} does not exist`);
|
||||
throw new lib.NotFoundError(`${oldName} does not exist`);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
@@ -1107,7 +1103,7 @@ export default class Host extends lib.Link {
|
||||
);
|
||||
} catch (err: any) {
|
||||
if (err.code === "ENOENT") {
|
||||
throw new lib.RequestError(`${source} does not exist`);
|
||||
throw new lib.NotFoundError(`${source} does not exist`);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
@@ -1156,7 +1152,7 @@ export default class Host extends lib.Link {
|
||||
} catch (err: any) {
|
||||
// ENOENT may also come from a missing target saves folder.
|
||||
if (err.code === "ENOENT" && !await fs.access(sourcePath).then(() => true, () => false)) {
|
||||
throw new lib.RequestError(`${sourceName} does not exist`);
|
||||
throw new lib.NotFoundError(`${sourceName} does not exist`);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
@@ -1188,7 +1184,7 @@ export default class Host extends lib.Link {
|
||||
await fs.unlink(path.join(instance.path, "saves", name));
|
||||
} catch (err: any) {
|
||||
if (err.code === "ENOENT") {
|
||||
throw new lib.RequestError(`${name} does not exist`);
|
||||
throw new lib.NotFoundError(`${name} does not exist`);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
@@ -1219,7 +1215,7 @@ export default class Host extends lib.Link {
|
||||
content = (await fs.open(path.join(instance.path, "saves", name))).createReadStream();
|
||||
} catch (err: any) {
|
||||
if (err.code === "ENOENT") {
|
||||
throw new lib.RequestError(`${name} does not exist`);
|
||||
throw new lib.NotFoundError(`${name} does not exist`);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
@@ -1238,12 +1234,12 @@ export default class Host extends lib.Link {
|
||||
async handleInstanceDeleteInternalRequest(request: lib.InstanceDeleteInternalRequest) {
|
||||
let instanceId = request.instanceId;
|
||||
if (this.instanceConnections.has(instanceId)) {
|
||||
throw new lib.RequestError(`Instance with ID ${instanceId} is running`);
|
||||
throw new lib.ConflictError(`Instance with ID ${instanceId} is running`);
|
||||
}
|
||||
|
||||
let instance = this.discoveredInstances.get(instanceId);
|
||||
if (!instance) {
|
||||
throw new lib.RequestError(`Instance with ID ${instanceId} does not exist`);
|
||||
throw new lib.NotFoundError(`Instance with ID ${instanceId} does not exist`);
|
||||
}
|
||||
|
||||
this.discoveredInstances.delete(instanceId);
|
||||
@@ -1253,21 +1249,21 @@ export default class Host extends lib.Link {
|
||||
|
||||
async handleHostUpdateRequest(request: lib.HostUpdateRequest) {
|
||||
if (!this.config.get("host.allow_remote_updates")) {
|
||||
throw new lib.RequestError("Remote updates are disabled on this machine");
|
||||
throw new lib.ForbiddenError("Remote updates are disabled on this machine");
|
||||
}
|
||||
return await lib.updatePackage("@clusterio/host");
|
||||
}
|
||||
|
||||
async handlePluginUpdateRequest(request: lib.PluginUpdateRequest) {
|
||||
if (!this.config.get("host.allow_plugin_updates")) {
|
||||
throw new lib.RequestError("Plugin updates are disabled on this machine");
|
||||
throw new lib.ForbiddenError("Plugin updates are disabled on this machine");
|
||||
}
|
||||
return await lib.handlePluginUpdate(request.pluginPackage, this.pluginInfos);
|
||||
}
|
||||
|
||||
async handlePluginInstallRequest(request: lib.PluginInstallRequest) {
|
||||
if (!this.config.get("host.allow_plugin_install")) {
|
||||
throw new lib.RequestError("Plugin installs are disabled on this machine");
|
||||
throw new lib.ForbiddenError("Plugin installs are disabled on this machine");
|
||||
}
|
||||
const npmPackage = await lib.handlePluginInstall(request.pluginPackage);
|
||||
// A new plugin is not in pluginInfos, so checkRestartRequired cannot notice it. Version
|
||||
@@ -1279,10 +1275,10 @@ export default class Host extends lib.Link {
|
||||
|
||||
async handleUpdateAllRequest(request: lib.UpdateAllRequest) {
|
||||
if (!this.config.get("host.allow_remote_updates")) {
|
||||
throw new lib.RequestError("Remote updates are disabled on this machine");
|
||||
throw new lib.ForbiddenError("Remote updates are disabled on this machine");
|
||||
}
|
||||
if (!this.config.get("host.allow_plugin_updates")) {
|
||||
throw new lib.RequestError("Plugin updates are disabled on this machine");
|
||||
throw new lib.ForbiddenError("Plugin updates are disabled on this machine");
|
||||
}
|
||||
return await lib.handleUpdateAll("@clusterio/host", this.pluginInfos);
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ function checkRequestSaveName(name: string) {
|
||||
try {
|
||||
lib.checkFilename(name);
|
||||
} catch (err: any) {
|
||||
throw new lib.RequestError(`Save name ${err.message}`);
|
||||
throw new lib.ValidationError(`Save name ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -952,12 +952,12 @@ end`.replace(/\r?\n/g, " ");
|
||||
* Factorio refuses to read it if it is newer than the game. It then
|
||||
* loads default settings instead, which can corrupt the save.
|
||||
*
|
||||
* @throws {lib.RequestError} if the mod pack version is higher than the server version.
|
||||
* @throws {lib.ConflictError} if the mod pack version is higher than the server version.
|
||||
*/
|
||||
checkModPackVersion(modPack: lib.ModPack) {
|
||||
const serverVersion = this.server.version;
|
||||
if (serverVersion && modPack.integerFactorioVersion > lib.integerFullVersion(serverVersion)) {
|
||||
throw new lib.RequestError(
|
||||
throw new lib.ConflictError(
|
||||
`Mod pack ${modPack.name} is for Factorio ${modPack.factorioVersion} ` +
|
||||
`which is newer than the Factorio ${serverVersion} this instance runs`
|
||||
);
|
||||
@@ -1360,7 +1360,7 @@ end`.replace(/\r?\n/g, " ");
|
||||
async handleInstanceLoadScenarioRequest(request: lib.InstanceLoadScenarioRequest) {
|
||||
if (this.config.get("factorio.enable_save_patching")) {
|
||||
this.notifyExit();
|
||||
throw new lib.RequestError("Load scenario cannot be used with save patching enabled");
|
||||
throw new lib.ConflictError("Load scenario cannot be used with save patching enabled");
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -1422,7 +1422,7 @@ end`.replace(/\r?\n/g, " ");
|
||||
|
||||
async handleInstanceSaveGameRequest() {
|
||||
if (this._status !== "running") {
|
||||
throw new lib.RequestError("Instance is not running");
|
||||
throw new lib.ConflictError("Instance is not running");
|
||||
}
|
||||
|
||||
const saved = new Promise<void>((resolve, reject) => {
|
||||
@@ -1432,7 +1432,7 @@ end`.replace(/\r?\n/g, " ");
|
||||
};
|
||||
const onExit = () => {
|
||||
this.server.off("save-finished", onSaved);
|
||||
reject(new lib.RequestError("Instance stopped before the save finished"));
|
||||
reject(new lib.ConflictError("Instance stopped before the save finished"));
|
||||
};
|
||||
this.server.once("save-finished", onSaved);
|
||||
this.server.once("exit", onExit);
|
||||
|
||||
@@ -9,43 +9,12 @@ import * as libSchema from "../schema.js";
|
||||
import { StringEnum } from "../data/composites.js";
|
||||
import { safeOutputFile } from "../file_ops.js";
|
||||
import * as validators from "./validators.js";
|
||||
import { RequestError } from "../errors.js";
|
||||
import { InvalidAccessError, InvalidFieldError, InvalidValueError } from "../errors.js";
|
||||
import type { SyncHook } from "../SyncHook.js";
|
||||
|
||||
const ConfigLocation = StringEnum(["controller", "host", "control"]);
|
||||
export type ConfigLocation = Static<typeof ConfigLocation>;
|
||||
|
||||
/**
|
||||
* Invalid Access exception
|
||||
*
|
||||
* Exception class for when an access attempt is made to a field that
|
||||
* does not permitt this access is done.
|
||||
*
|
||||
* @extends Error
|
||||
*/
|
||||
export class InvalidAccess extends RequestError { };
|
||||
|
||||
/**
|
||||
* Invalid Value exception
|
||||
*
|
||||
* Exception class for when invalid values are attempted to be set on a
|
||||
* field.
|
||||
*
|
||||
* @extends Error
|
||||
*/
|
||||
export class InvalidValue extends RequestError { };
|
||||
|
||||
/**
|
||||
* Invalid Field exception
|
||||
*
|
||||
* Exception class for when a group or field that does not exist is
|
||||
* attempted to be accessed.
|
||||
*
|
||||
* @extends Error
|
||||
*/
|
||||
export class InvalidField extends RequestError { };
|
||||
|
||||
|
||||
const OldConfigGroupSchema = Type.Object({
|
||||
"name": Type.String(),
|
||||
"fields": Type.Record(Type.String(), Type.Unknown()),
|
||||
@@ -368,7 +337,7 @@ export class Config<
|
||||
* @param name - Name of the field being validated.
|
||||
* @param def - Definition describing the field type and constraints.
|
||||
* @param value - Value to validate and coerce.
|
||||
* @throws {InvalidValue} if the value cannot be coerced to the correct type
|
||||
* @throws {InvalidValueError} if the value cannot be coerced to the correct type
|
||||
* @returns The validated and coerced value.
|
||||
* @private
|
||||
*/
|
||||
@@ -380,7 +349,7 @@ export class Config<
|
||||
|
||||
// If def is an enum then it must be a value within it
|
||||
if (def.enum && !def.enum.includes(value)) {
|
||||
throw new InvalidValue(`Expected one of [${def.enum.join(", ")}], not ${value}`);
|
||||
throw new InvalidValueError(`Expected one of [${def.enum.join(", ")}], not ${value}`);
|
||||
}
|
||||
|
||||
// If it is a string, then attempt to coerce it
|
||||
@@ -402,7 +371,7 @@ export class Config<
|
||||
try {
|
||||
value = JSON.parse(value);
|
||||
} catch (err: any) {
|
||||
throw new InvalidValue(`Error parsing value for ${name}: ${err.message}`);
|
||||
throw new InvalidValueError(`Error parsing value for ${name}: ${err.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -410,12 +379,12 @@ export class Config<
|
||||
// Check if it is allowed to be null
|
||||
if (value === null) {
|
||||
if (!def.optional) {
|
||||
throw new InvalidValue(`Field ${name} cannot be null`);
|
||||
throw new InvalidValueError(`Field ${name} cannot be null`);
|
||||
}
|
||||
|
||||
// Check if is the correct type
|
||||
} else if (basicType(value) !== def.type) {
|
||||
throw new InvalidValue(`Expected type of ${name} to be ${def.type}, not ${basicType(value)}`);
|
||||
throw new InvalidValueError(`Expected type of ${name} to be ${def.type}, not ${basicType(value)}`);
|
||||
}
|
||||
|
||||
return value as FieldType<T["type"]>;
|
||||
@@ -429,7 +398,7 @@ export class Config<
|
||||
* @param location - location to check access for.
|
||||
* @param mode - type of access to check for.
|
||||
* @param error - throw on failure to pass check.
|
||||
* @throws {InvalidAccess}
|
||||
* @throws {InvalidAccessError}
|
||||
* if access is not granted and error is true.
|
||||
* @returns true if access is granted
|
||||
* @private
|
||||
@@ -448,7 +417,7 @@ export class Config<
|
||||
)
|
||||
) {
|
||||
if (error) {
|
||||
throw new InvalidAccess(`Field '${name}' is not accessible from ${location}`);
|
||||
throw new InvalidAccessError(`Field '${name}' is not accessible from ${location}`);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
@@ -648,7 +617,7 @@ export class Config<
|
||||
}
|
||||
let def = this.constructor.fieldDefinitions[name];
|
||||
if (!def) {
|
||||
throw new InvalidField(`No field named '${name}'`);
|
||||
throw new InvalidFieldError(`No field named '${name}'`);
|
||||
}
|
||||
return this._checkAccess(name, def, location, mode, false);
|
||||
}
|
||||
@@ -663,7 +632,7 @@ export class Config<
|
||||
get<Field extends keyof Fields & string>(name: Field, remote = this.location) {
|
||||
let def = this.constructor.fieldDefinitions[name];
|
||||
if (!def) {
|
||||
throw new InvalidField(`No field named '${name}'`);
|
||||
throw new InvalidFieldError(`No field named '${name}'`);
|
||||
}
|
||||
// If remote access is requested require it to be able to read the field
|
||||
if (remote !== this.location) {
|
||||
@@ -675,7 +644,7 @@ export class Config<
|
||||
!this._checkAccess(name, def, this.location, ConfigAccess.write, false)
|
||||
&& !this._checkAccess(name, def, this.location, ConfigAccess.read, false)
|
||||
) {
|
||||
throw new InvalidAccess(`Field '${name}' is not accessible from ${this.location}`);
|
||||
throw new InvalidAccessError(`Field '${name}' is not accessible from ${this.location}`);
|
||||
}
|
||||
|
||||
return this.fields[name];
|
||||
@@ -690,13 +659,13 @@ export class Config<
|
||||
* @param name - Name of field to set.
|
||||
* @param newValue - Value to set for field.
|
||||
* @param remote - Location this field is being set from.
|
||||
* @throws {InvalidField} if field is not defined.
|
||||
* @throws {InvalidValue} if value is not allowed for the field.
|
||||
* @throws {InvalidFieldError} if field is not defined.
|
||||
* @throws {InvalidValueError} if value is not allowed for the field.
|
||||
*/
|
||||
set<Field extends keyof Fields & string>(name: Field, newValue: Fields[Field], remote = this.location) {
|
||||
const def = this.constructor.fieldDefinitions[name];
|
||||
if (!def) {
|
||||
throw new InvalidField(`No field named '${name}'`);
|
||||
throw new InvalidFieldError(`No field named '${name}'`);
|
||||
}
|
||||
|
||||
this._checkAccess(name, def, remote, ConfigAccess.write, true);
|
||||
@@ -714,7 +683,7 @@ export class Config<
|
||||
try {
|
||||
def.validator(value, this);
|
||||
} catch (err: any) {
|
||||
throw new InvalidValue(`Failed validation of ${name}: ${err.message}`);
|
||||
throw new InvalidValueError(`Failed validation of ${name}: ${err.message}`);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
@@ -732,7 +701,7 @@ export class Config<
|
||||
try {
|
||||
dependent.def.validator(this.get(dependent.name as any, remote), this.staging);
|
||||
} catch (err: any) {
|
||||
throw new InvalidValue(`Failed validation of dependent ${dependent.name}: ${err.message}`);
|
||||
throw new InvalidValueError(`Failed validation of dependent ${dependent.name}: ${err.message}`);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
@@ -753,8 +722,8 @@ export class Config<
|
||||
* @param prop - Name of property to set on field.
|
||||
* @param value - the value to set the property to.
|
||||
* @param remote - Location this property is being set from
|
||||
* @throws {InvalidField} if field is not defined.
|
||||
* @throws {InvalidValue} if field is not an object.
|
||||
* @throws {InvalidFieldError} if field is not defined.
|
||||
* @throws {InvalidValueError} if field is not an object.
|
||||
*/
|
||||
setProp<Field extends keyof Fields & string>(
|
||||
name: Field,
|
||||
@@ -764,7 +733,7 @@ export class Config<
|
||||
) {
|
||||
let def = this.constructor.fieldDefinitions[name];
|
||||
if (!def) {
|
||||
throw new InvalidField(`No field named '${name}'`);
|
||||
throw new InvalidFieldError(`No field named '${name}'`);
|
||||
}
|
||||
|
||||
this._checkAccess(name, def, remote, ConfigAccess.write, true);
|
||||
@@ -773,7 +742,7 @@ export class Config<
|
||||
}
|
||||
|
||||
if (def.type !== "object") {
|
||||
throw new InvalidValue(`Cannot set property on non-object field '${name}'`);
|
||||
throw new InvalidValueError(`Cannot set property on non-object field '${name}'`);
|
||||
}
|
||||
|
||||
let prev = this.fields[name] as Record<string, unknown>;
|
||||
@@ -793,7 +762,7 @@ export class Config<
|
||||
try {
|
||||
def.validator(updated, this);
|
||||
} catch (err: any) {
|
||||
throw new InvalidValue(`Failed validation of ${name}: ${err.message}`);
|
||||
throw new InvalidValueError(`Failed validation of ${name}: ${err.message}`);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
@@ -811,7 +780,7 @@ export class Config<
|
||||
try {
|
||||
dependent.def.validator(this.get(dependent.name as any, remote), this.staging);
|
||||
} catch (err: any) {
|
||||
throw new InvalidValue(`Failed validation of dependent ${dependent.name}: ${err.message}`);
|
||||
throw new InvalidValueError(`Failed validation of dependent ${dependent.name}: ${err.message}`);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
@@ -829,8 +798,8 @@ export class Config<
|
||||
* @param name - Name of field to set.
|
||||
* @param newValue - Value to set for field.
|
||||
* @param remote - Location this field is being set from.
|
||||
* @throws {InvalidField} if field is not defined.
|
||||
* @throws {InvalidValue} if value is not allowed for the field.
|
||||
* @throws {InvalidFieldError} if field is not defined.
|
||||
* @throws {InvalidValueError} if value is not allowed for the field.
|
||||
*/
|
||||
stage<Field extends keyof Fields & string>(name: Field, newValue: Fields[Field], remote = this.location) {
|
||||
if (this._readonly) {
|
||||
@@ -839,7 +808,7 @@ export class Config<
|
||||
|
||||
const def = this.constructor.fieldDefinitions[name];
|
||||
if (!def) {
|
||||
throw new InvalidField(`No field named '${name}'`);
|
||||
throw new InvalidFieldError(`No field named '${name}'`);
|
||||
}
|
||||
|
||||
this._checkAccess(name, def, remote, ConfigAccess.write, true);
|
||||
@@ -859,8 +828,8 @@ export class Config<
|
||||
* @param prop - Name of property to set on field.
|
||||
* @param value - the value to set the property to.
|
||||
* @param remote - Location this property is being set from
|
||||
* @throws {InvalidField} if field is not defined.
|
||||
* @throws {InvalidValue} if field is not an object.
|
||||
* @throws {InvalidFieldError} if field is not defined.
|
||||
* @throws {InvalidValueError} if field is not an object.
|
||||
*/
|
||||
stageProp<Field extends keyof Fields & string>(
|
||||
name: Field,
|
||||
@@ -874,7 +843,7 @@ export class Config<
|
||||
|
||||
const def = this.constructor.fieldDefinitions[name];
|
||||
if (!def) {
|
||||
throw new InvalidField(`No field named '${name}'`);
|
||||
throw new InvalidFieldError(`No field named '${name}'`);
|
||||
}
|
||||
|
||||
this._checkAccess(name, def, remote, ConfigAccess.write, true);
|
||||
@@ -883,7 +852,7 @@ export class Config<
|
||||
}
|
||||
|
||||
if (def.type !== "object") {
|
||||
throw new InvalidValue(`Cannot set property on non-object field '${name}'`);
|
||||
throw new InvalidValueError(`Cannot set property on non-object field '${name}'`);
|
||||
}
|
||||
|
||||
const prev = this.staging.get(name) as Record<string, unknown>;
|
||||
@@ -901,7 +870,7 @@ export class Config<
|
||||
/**
|
||||
* Commit all changes in stating after they have been validated.
|
||||
*
|
||||
* @throws {InvalidValue} if a value is not allowed for its field.
|
||||
* @throws {InvalidValueError} if a value is not allowed for its field.
|
||||
*/
|
||||
commitStaging() {
|
||||
if (this._readonly) {
|
||||
@@ -923,7 +892,7 @@ export class Config<
|
||||
try {
|
||||
fieldDef.validator(config.fields[fieldName as keyof Fields], config);
|
||||
} catch (err: any) {
|
||||
throw new InvalidValue(`Failed validation of ${fieldName}: ${err.message}`);
|
||||
throw new InvalidValueError(`Failed validation of ${fieldName}: ${err.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,7 +162,7 @@ export interface IUser extends UserDetails {
|
||||
*
|
||||
* @param permission - The permission to check for.
|
||||
* @throws {Error} If the given permission does not exist.
|
||||
* @throws {libErrors.PermissionError} if the user does noh have the given permission.
|
||||
* @throws {libErrors.PermissionDeniedError} if the user does noh have the given permission.
|
||||
*/
|
||||
checkPermission(permission: PermissionName): void
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import { Type, Static } from "@sinclair/typebox";
|
||||
import UserDetails, { IUser } from "./UserDetails.js";
|
||||
import { StringEnum, jsonArray, plainJson } from "./composites.js";
|
||||
import { MessageRequest } from "./messages_core.js";
|
||||
import { PermissionError } from "../errors.js";
|
||||
import { ValidationError } from "../errors.js";
|
||||
|
||||
export class UserGetRequest {
|
||||
declare ["constructor"]: typeof UserGetRequest;
|
||||
@@ -277,9 +277,9 @@ export class UserBulkImportRequest {
|
||||
user.checkPermission("core.user.set_whitelisted");
|
||||
break;
|
||||
default:
|
||||
// Unvalidated input during permission check: deny, don't throw a bare Error
|
||||
// Unvalidated input during permission check: reject, don't throw a bare Error
|
||||
// @ts-expect-error Unreachable
|
||||
throw new PermissionError(`Unknown import / restore type: ${data.importType}`);
|
||||
throw new ValidationError(`Unknown import / restore type: ${data.importType}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+61
-25
@@ -18,40 +18,76 @@ export class InstallationError extends Error {
|
||||
}
|
||||
|
||||
/**
|
||||
* Thrown from requests sent when an error occured handling it
|
||||
* Base class for errors caused by the request, such as invalid input,
|
||||
* missing permissions or a missing resource. Corresponds to the HTTP 4xx
|
||||
* range.
|
||||
*
|
||||
* Link does not log these as unexpected when a request handler throws them.
|
||||
*/
|
||||
export class RequestError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
public code = "RequestError",
|
||||
public stack?: string,
|
||||
) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
export class ClientError extends Error { }
|
||||
|
||||
/**
|
||||
* Thrown when a permission check fails.
|
||||
*
|
||||
* Is a subclass of RequestError to prevent logging stack traces when
|
||||
* requests fail due to permission denied.
|
||||
*
|
||||
* Thrown when the request contains invalid data (HTTP 400)
|
||||
*/
|
||||
export class PermissionError extends RequestError {
|
||||
constructor(
|
||||
message: string,
|
||||
code = "PermissionError",
|
||||
stack?: string,
|
||||
) {
|
||||
super(message, code, stack);
|
||||
}
|
||||
export class ValidationError extends ClientError { }
|
||||
|
||||
}
|
||||
/**
|
||||
* Thrown when a config value cannot be used for its field
|
||||
*/
|
||||
export class InvalidValueError extends ValidationError { }
|
||||
|
||||
/**
|
||||
* Thrown when a config field does not exist
|
||||
*/
|
||||
export class InvalidFieldError extends ValidationError { }
|
||||
|
||||
/**
|
||||
* Thrown when the request is not allowed (HTTP 403)
|
||||
*/
|
||||
export class ForbiddenError extends ClientError { }
|
||||
|
||||
/**
|
||||
* Thrown when a permission check fails
|
||||
*/
|
||||
export class PermissionDeniedError extends ForbiddenError { }
|
||||
|
||||
/**
|
||||
* Thrown when a config field is not accessible from the current location
|
||||
*/
|
||||
export class InvalidAccessError extends ForbiddenError { }
|
||||
|
||||
/**
|
||||
* Thrown when a resource the request refers to does not exist (HTTP 404)
|
||||
*/
|
||||
export class NotFoundError extends ClientError { }
|
||||
|
||||
/**
|
||||
* Thrown when an operation for a request timed out (HTTP 408)
|
||||
*/
|
||||
export class TimeoutError extends ClientError { }
|
||||
|
||||
/**
|
||||
* Thrown when the request conflicts with the current state, such as an
|
||||
* instance that is running or a host that is not connected (HTTP 409)
|
||||
*/
|
||||
export class ConflictError extends ClientError { }
|
||||
|
||||
/**
|
||||
* Base class for errors caused by the side handling the request.
|
||||
* Corresponds to the HTTP 5xx range.
|
||||
*/
|
||||
export class ServerError extends Error { }
|
||||
|
||||
/**
|
||||
* Received in place of an error response whose class is not registered,
|
||||
* such as a TypeError thrown by a request handler (HTTP 500)
|
||||
*/
|
||||
export class InternalServerError extends ServerError { }
|
||||
|
||||
/**
|
||||
* Signal for messages that fail validation
|
||||
*/
|
||||
export class InvalidMessage extends Error {
|
||||
export class InvalidMessage extends ValidationError {
|
||||
code = "InvalidMessage";
|
||||
constructor(
|
||||
message: string,
|
||||
|
||||
+101
-26
@@ -3,7 +3,7 @@ import * as libData from "../data/index.js";
|
||||
import * as libErrors from "../errors.js";
|
||||
import { logger } from "../logging.js";
|
||||
import * as libSchema from "../schema.js";
|
||||
import { dataClasses } from "./messages.js";
|
||||
import { dataClasses, errorClasses } from "./messages.js";
|
||||
import { BaseConnector, WebSocketBaseConnector } from "./connectors.js";
|
||||
import { strict as assert } from "assert";
|
||||
import type { PluginNodeEnvInfo, PluginWebpackEnvInfo } from "../plugin.js";
|
||||
@@ -38,6 +38,12 @@ export interface Event<T> {
|
||||
}
|
||||
export type EventClass<T> = Event<T>["constructor"];
|
||||
|
||||
export interface ErrorClass {
|
||||
new (message: string): Error;
|
||||
name: string;
|
||||
plugin?: string;
|
||||
}
|
||||
|
||||
export interface RequestEntry {
|
||||
Request: RequestClass<unknown, unknown>;
|
||||
name: string,
|
||||
@@ -156,7 +162,7 @@ export class Link {
|
||||
return this._pendingRequests.size + this._forwardedRequests.size;
|
||||
}
|
||||
|
||||
_clearPendingRequests(err: Error & { code?: string }) {
|
||||
_clearPendingRequests(err: Error) {
|
||||
for (let pending of this._pendingRequests.values()) {
|
||||
pending.reject(err);
|
||||
}
|
||||
@@ -170,9 +176,7 @@ export class Link {
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
pending.origin.connector.sendResponseError(
|
||||
new libData.ResponseError(err.message, err.code), pending.src, pending.dst
|
||||
);
|
||||
pending.origin.connector.sendResponseError(Link.errorToResponse(err, false), pending.src, pending.dst);
|
||||
}
|
||||
this._pendingRequests.clear();
|
||||
this._forwardedRequests.clear();
|
||||
@@ -200,10 +204,10 @@ export class Link {
|
||||
try {
|
||||
this.validatePermission(permissionMessage, entry); // Somewhat hacky, defined in ControlConnection
|
||||
} catch (err) {
|
||||
// A non-PermissionError means the permission check itself is
|
||||
// A non-ClientError means the permission check itself is
|
||||
// broken. validatePermission (control connections only) already
|
||||
// sent a response error, so log and drop instead of crashing.
|
||||
if (!(err instanceof libErrors.PermissionError)) {
|
||||
if (!(err instanceof libErrors.ClientError)) {
|
||||
logger.error(
|
||||
`Unexpected error checking permission for ${permissionMessage.name}:\n` +
|
||||
`${(err as Error).stack ?? (err as Error).message}`
|
||||
@@ -280,7 +284,7 @@ export class Link {
|
||||
this.validateIngress(message);
|
||||
} catch (err: any) {
|
||||
if (message.type === "request") {
|
||||
this.connector.sendResponseError(new libData.ResponseError(err.message, err.code), message.src);
|
||||
this.connector.sendResponseError(Link.errorToResponse(err, false), message.src);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
@@ -290,17 +294,17 @@ export class Link {
|
||||
let entry = Link._requestsByName.get(message.name);
|
||||
if (!entry) {
|
||||
let err = new libErrors.InvalidMessage(`Unrecognized request ${message.name}`);
|
||||
this.connector.sendResponseError(new libData.ResponseError(err.message, err.code), message.src);
|
||||
this.connector.sendResponseError(Link.errorToResponse(err, false), message.src);
|
||||
throw err;
|
||||
}
|
||||
if (!entry.allowedSrcTypes.has(message.src.type)) {
|
||||
let err = new libErrors.InvalidMessage(`Source ${message.src} is not allowed for ${message.name}`);
|
||||
this.connector.sendResponseError(new libData.ResponseError(err.message, err.code), message.src);
|
||||
this.connector.sendResponseError(Link.errorToResponse(err, false), message.src);
|
||||
throw err;
|
||||
}
|
||||
if (!entry.allowedDstTypes.has(message.dst.type)) {
|
||||
let err = new libErrors.InvalidMessage(`Destination ${message.dst} is not allowed for ${message.name}`);
|
||||
this.connector.sendResponseError(new libData.ResponseError(err.message, err.code), message.src);
|
||||
this.connector.sendResponseError(Link.errorToResponse(err, false), message.src);
|
||||
throw err;
|
||||
}
|
||||
return entry;
|
||||
@@ -344,7 +348,7 @@ export class Link {
|
||||
*
|
||||
* @param message - Message to check.
|
||||
* @param entry - Request or Event entry for this Message.
|
||||
* @throws {libErrors.PermissionError} if unauthorized.
|
||||
* @throws {libErrors.PermissionDeniedError} if unauthorized.
|
||||
*/
|
||||
validatePermission(message: libData.MessageRequest | libData.MessageEvent, entry: RequestEntry | EventEntry) { }
|
||||
|
||||
@@ -356,7 +360,7 @@ export class Link {
|
||||
);
|
||||
if (message.type === "request") {
|
||||
this.connector.sendResponseError(
|
||||
new libData.ResponseError(err.message, err.code), (message as libData.MessageRequest).src
|
||||
Link.errorToResponse(err, false), (message as libData.MessageRequest).src
|
||||
);
|
||||
}
|
||||
throw err;
|
||||
@@ -399,13 +403,7 @@ export class Link {
|
||||
try {
|
||||
response = handler(entry.requestFromJSON(message.data), message.src, message.dst);
|
||||
} catch (err: any) {
|
||||
if (err.errors) {
|
||||
logger.error(JSON.stringify(err.errors, null, "\t"));
|
||||
}
|
||||
this.connector.sendResponseError(
|
||||
new libData.ResponseError(err.message, err.code, err.stack), message.src
|
||||
);
|
||||
return;
|
||||
response = Promise.reject(err);
|
||||
}
|
||||
|
||||
if (!(response instanceof Promise)) {
|
||||
@@ -424,19 +422,17 @@ export class Link {
|
||||
this.connector.sendResponse(result, message.src, spoofedSrc);
|
||||
}
|
||||
).catch(
|
||||
(err: Error & { code?: string }) => {
|
||||
(err: Error) => {
|
||||
if (err instanceof libErrors.InvalidMessage) {
|
||||
logger.error(err.message);
|
||||
if (err.errors) {
|
||||
logger.error(JSON.stringify(err.errors, null, "\t"));
|
||||
}
|
||||
} else if (!(err instanceof libErrors.RequestError)) {
|
||||
} else if (Link.errorName(err) === undefined) {
|
||||
logger.error(`Unexpected error responding to ${message.name}:\n${err.stack}`);
|
||||
}
|
||||
try {
|
||||
this.connector.sendResponseError(
|
||||
new libData.ResponseError(err.message, err.code, err.stack), message.src, spoofedSrc
|
||||
);
|
||||
this.connector.sendResponseError(Link.errorToResponse(err), message.src, spoofedSrc);
|
||||
} catch (subErr: any) {
|
||||
logger.error(`Unexpected error sending error response for ${message.name}:\n${subErr.stack}`);
|
||||
}
|
||||
@@ -474,7 +470,7 @@ export class Link {
|
||||
}
|
||||
|
||||
this._pendingRequests.delete(message.dst.requestId!);
|
||||
pending.reject(new libErrors.RequestError(message.data.message, message.data.code, message.data.stack));
|
||||
pending.reject(Link.errorFromResponse(message.data));
|
||||
}
|
||||
|
||||
_processEvent(message: libData.MessageEvent, entry: EventEntry) {
|
||||
@@ -823,11 +819,81 @@ export class Link {
|
||||
this._eventsByName.set(name, entry);
|
||||
this._eventsByClass.set(Event as EventClass<unknown>, entry);
|
||||
}
|
||||
|
||||
static _errorsByName = new Map<string, ErrorClass>();
|
||||
static _errorNamesByClass = new Map<ErrorClass, string>();
|
||||
|
||||
/**
|
||||
* Register an error class to be sent and received over links
|
||||
*
|
||||
* Error responses carry the registered name of the thrown error and the
|
||||
* receiving side rejects the request with an instance of the same class.
|
||||
*
|
||||
* @param Class - Error class, must take the message as the first
|
||||
* constructor argument.
|
||||
*/
|
||||
static registerError(Class: ErrorClass) {
|
||||
const name = Class.plugin ? `${Class.plugin}:${Class.name}` : Class.name;
|
||||
if (this._errorsByName.has(name)) {
|
||||
throw new Error(`Error ${name} is already registered`);
|
||||
}
|
||||
this._errorsByName.set(name, Class);
|
||||
this._errorNamesByClass.set(Class, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registered name of the closest registered class of an error
|
||||
*
|
||||
* @param err - Error to look up.
|
||||
* @returns name or undefined if no class in its prototype chain is registered.
|
||||
*/
|
||||
static errorName(err: Error) {
|
||||
for (let proto = Object.getPrototypeOf(err); proto; proto = Object.getPrototypeOf(proto)) {
|
||||
const name = this._errorNamesByClass.get(proto.constructor);
|
||||
if (name) {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an error into the data of an error response
|
||||
*
|
||||
* Errors with no registered class are sent as InternalServerError.
|
||||
*
|
||||
* @param err - Error to convert.
|
||||
* @param withStack - Include the stack trace of the error.
|
||||
*/
|
||||
static errorToResponse(err: Error, withStack = true) {
|
||||
return new libData.ResponseError(
|
||||
err.message, this.errorName(err) ?? "InternalServerError", withStack ? err.stack : undefined
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the data of an error response back into an error
|
||||
*
|
||||
* @param response - Received error response data.
|
||||
* @returns instance of the registered class, or InternalServerError if
|
||||
* the class is not registered on this side.
|
||||
*/
|
||||
static errorFromResponse(response: libData.ResponseError) {
|
||||
const Class = response.code && this._errorsByName.get(response.code) || libErrors.InternalServerError;
|
||||
const err = new Class(response.message);
|
||||
if (response.stack !== undefined) {
|
||||
err.stack = response.stack;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
for (let Class of dataClasses) {
|
||||
Link.register(Class as RequestClass<unknown, unknown>);
|
||||
}
|
||||
for (let Class of errorClasses) {
|
||||
Link.registerError(Class);
|
||||
}
|
||||
|
||||
export function registerPluginMessages(pluginInfos: PluginNodeEnvInfo[]|PluginWebpackEnvInfo[]) {
|
||||
for (let pluginInfo of pluginInfos) {
|
||||
@@ -840,5 +906,14 @@ export function registerPluginMessages(pluginInfos: PluginNodeEnvInfo[]|PluginWe
|
||||
}
|
||||
Link.register(Class);
|
||||
}
|
||||
for (let Class of pluginInfo.errors || []) {
|
||||
if (Class.plugin !== pluginInfo.name) {
|
||||
throw new Error(
|
||||
`Expected error ${Class.name} from ${pluginInfo.name} to have a static ` +
|
||||
`plugin property set to "${pluginInfo.name}"`
|
||||
);
|
||||
}
|
||||
Link.registerError(Class);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,8 @@ import * as role from "../data/messages_role.js";
|
||||
import * as note from "../data/messages_note.js";
|
||||
import * as plugin from "../data/messages_plugin.js";
|
||||
import * as subscriptions from "../subscriptions.js";
|
||||
import type { RequestClass, EventClass } from "./link.js";
|
||||
import * as errors from "../errors.js";
|
||||
import type { RequestClass, EventClass, ErrorClass } from "./link.js";
|
||||
|
||||
/**
|
||||
* Event and Request payloads registered with the Link class. Used to decode
|
||||
@@ -151,3 +152,25 @@ export const dataClasses: (RequestClass<unknown, unknown> | EventClass<unknown>)
|
||||
plugin.PluginLatestVersionsRequest,
|
||||
plugin.UpdateAllRequest,
|
||||
];
|
||||
|
||||
/**
|
||||
* Error classes registered with the Link class. Used to send errors thrown
|
||||
* by request handlers back as instances of the same class.
|
||||
* @internal
|
||||
*/
|
||||
export const errorClasses: ErrorClass[] = [
|
||||
errors.ClientError,
|
||||
errors.ValidationError,
|
||||
errors.InvalidValueError,
|
||||
errors.InvalidFieldError,
|
||||
errors.ForbiddenError,
|
||||
errors.PermissionDeniedError,
|
||||
errors.InvalidAccessError,
|
||||
errors.NotFoundError,
|
||||
errors.TimeoutError,
|
||||
errors.ConflictError,
|
||||
errors.ServerError,
|
||||
errors.InternalServerError,
|
||||
errors.InvalidMessage,
|
||||
errors.SessionLost,
|
||||
];
|
||||
@@ -8,6 +8,7 @@ import type { ConfigSchema, FieldDefinition } from "./config/index.js";
|
||||
import type { PermissionDefinition } from "./permissions.js";
|
||||
import type { PlayerStats, Role } from "./data/index.js";
|
||||
import type { SyncHook } from "./SyncHook.js";
|
||||
import type { ErrorClass } from "./link/index.js";
|
||||
|
||||
export const PluginFeatureFlags = [
|
||||
/** The plugin requires module code to be patched into the save */
|
||||
@@ -42,6 +43,7 @@ export type PluginDeclaration = {
|
||||
features?: (typeof PluginFeatureFlags)[number][];
|
||||
|
||||
messages?: any[];
|
||||
errors?: ErrorClass[];
|
||||
permissions?: PermissionDefinition[];
|
||||
routes?: string[];
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import util from "util";
|
||||
import path from "path";
|
||||
import { exec } from "child_process";
|
||||
import { logger } from "./logging.js";
|
||||
import { RequestError } from "./errors.js";
|
||||
import { ConflictError, NotFoundError, UnavailableError } from "./errors.js";
|
||||
import type { PluginNodeEnvInfo } from "./plugin.js";
|
||||
import { isNonRegistryPackage } from "./load_plugin_list.js";
|
||||
import { LockFile, LockFileExistsError, LockFileRecoveryError } from "./LockFile.js";
|
||||
@@ -31,7 +31,7 @@ let operationInProgress = false;
|
||||
async function logExec(cmd: string, check?: () => Promise<void>) {
|
||||
// Checked and set before the first await so concurrent calls in this process can't interleave
|
||||
if (operationInProgress) {
|
||||
throw new RequestError(operationInProgressMessage);
|
||||
throw new ConflictError(operationInProgressMessage);
|
||||
}
|
||||
operationInProgress = true;
|
||||
const lockPath = path.resolve("npm-operation.lock");
|
||||
@@ -43,13 +43,13 @@ async function logExec(cmd: string, check?: () => Promise<void>) {
|
||||
await lock.acquire();
|
||||
} catch (err) {
|
||||
if (err instanceof LockFileRecoveryError) {
|
||||
throw new RequestError(err.message);
|
||||
throw new UnavailableError(err.message);
|
||||
}
|
||||
if (!(err instanceof LockFileExistsError)) {
|
||||
throw err;
|
||||
}
|
||||
const holder = await lock.holderPid();
|
||||
throw new RequestError(
|
||||
throw new ConflictError(
|
||||
`${operationInProgressMessage}${holder ? ` (${lockPath} is held by process ${holder})` : ""}`
|
||||
);
|
||||
}
|
||||
@@ -90,7 +90,7 @@ export async function handleUpdateAll(corePackage: string, pluginInfos: PluginNo
|
||||
/** Throws if the dependency by this name is a path, git, url or alias that installing by name would replace */
|
||||
async function checkNotLocalDependency(name: string) {
|
||||
if (await isNonRegistryPackage(name)) {
|
||||
throw new RequestError(`${name} is not installed from npm on this machine`);
|
||||
throw new ConflictError(`${name} is not installed from npm on this machine`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,7 +98,7 @@ export async function handlePluginUpdate(pluginName: string, pluginInfos: Plugin
|
||||
// Optionally pinned to a version as name@version to update past the range in package.json
|
||||
const { name, version } = parsePackageSpec(pluginName);
|
||||
if (!pluginInfos.some(plugin => plugin.npmPackage === name)) {
|
||||
throw new RequestError(`Plugin ${name} is not installed on this machine`);
|
||||
throw new NotFoundError(`Plugin ${name} is not installed on this machine`);
|
||||
}
|
||||
|
||||
if (version === undefined) {
|
||||
|
||||
@@ -9,6 +9,7 @@ import { spawn } from "child_process";
|
||||
import { loadPluginList } from "./load_plugin_list.js";
|
||||
import * as libConfig from "./config/index.js";
|
||||
import * as libFileOps from "./file_ops.js";
|
||||
import { InvalidFieldError, InvalidValueError } from "./errors.js";
|
||||
import { logger } from "./logging.js";
|
||||
import * as libHelpers from "./helpers.js";
|
||||
import { LockFile } from "./LockFile.js";
|
||||
@@ -271,7 +272,7 @@ export async function handleConfigCommand(
|
||||
try {
|
||||
print(instance.get(args.field as string));
|
||||
} catch (err) {
|
||||
if (err instanceof libConfig.InvalidField) {
|
||||
if (err instanceof InvalidFieldError) {
|
||||
logger.error(err.message);
|
||||
} else {
|
||||
throw err;
|
||||
@@ -292,7 +293,7 @@ export async function handleConfigCommand(
|
||||
instance.set(args.field as string, args.value);
|
||||
await instance.save();
|
||||
} catch (err) {
|
||||
if (err instanceof libConfig.InvalidField || err instanceof libConfig.InvalidValue) {
|
||||
if (err instanceof InvalidFieldError || err instanceof InvalidValueError) {
|
||||
logger.error(err.message);
|
||||
} else {
|
||||
throw err;
|
||||
|
||||
@@ -3,7 +3,7 @@ import { Link, Event, EventClass, RequestHandler, WebSocketBaseConnector } from
|
||||
import { Address, MessageRequest, IUser, JsonBoolean, StringEnum } from "./data/index.js";
|
||||
import isDeepStrictEqual from "#is_deep_strict_equal";
|
||||
import type { PermissionName } from "./permissions.js";
|
||||
import { RequestError } from "./errors.js";
|
||||
import { SessionLost, ValidationError } from "./errors.js";
|
||||
import { logger } from "./logging.js";
|
||||
|
||||
export type SubscriptionRequestHandler<T> = RequestHandler<SubscriptionRequest, Event<T> | null>;
|
||||
@@ -351,7 +351,7 @@ export class SubscriptionController {
|
||||
}
|
||||
}
|
||||
if (count > this._maxFilters) {
|
||||
throw new RequestError(`Subscription exceeds the limit of ${this._maxFilters} filters`);
|
||||
throw new ValidationError(`Subscription exceeds the limit of ${this._maxFilters} filters`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -685,7 +685,7 @@ export class EventSubscriber<E, S = null> {
|
||||
this._notify(null);
|
||||
}
|
||||
} catch (err: any) {
|
||||
if (!(err instanceof RequestError) || err.code !== "SessionLost") {
|
||||
if (!(err instanceof SessionLost)) {
|
||||
logger.error(`Unexpected error updating ${entry.name} subscription:\n${err.stack}`);
|
||||
}
|
||||
if (err instanceof Error) {
|
||||
|
||||
Reference in new issue
Block a user