mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-23 16:23:12 +09:00
Use the new error classes in code added on master
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
54e9340e61
commit
1a03e66479
4 files changed
+16
-16
No files matched your search
@@ -200,7 +200,7 @@ export default class ControlConnection extends BaseConnection {
|
|||||||
throw new lib.ConflictError("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()) {
|
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._controller.checkRestartDowngrade();
|
const downgrade = await this._controller.checkRestartDowngrade();
|
||||||
if (downgrade) {
|
if (downgrade) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { logger } from "./logging.js";
|
import { logger } from "./logging.js";
|
||||||
import { RequestError } from "./errors.js";
|
import { NotFoundError, UnavailableError, ValidationError } from "./errors.js";
|
||||||
import { PluginSearchRequest, PluginSearchResult } from "./data/messages_plugin.js";
|
import { PluginSearchRequest, PluginSearchResult } from "./data/messages_plugin.js";
|
||||||
|
|
||||||
const registryTimeoutMs = 10e3;
|
const registryTimeoutMs = 10e3;
|
||||||
@@ -19,10 +19,10 @@ export function parsePackageSpec(spec: string) {
|
|||||||
? [spec.slice(0, versionSeparator), spec.slice(versionSeparator + 1)]
|
? [spec.slice(0, versionSeparator), spec.slice(versionSeparator + 1)]
|
||||||
: [spec, undefined];
|
: [spec, undefined];
|
||||||
if (name.length > 214 || !packageNamePattern.test(name)) {
|
if (name.length > 214 || !packageNamePattern.test(name)) {
|
||||||
throw new RequestError(`Invalid plugin name: ${spec}`);
|
throw new ValidationError(`Invalid plugin name: ${spec}`);
|
||||||
}
|
}
|
||||||
if (version !== undefined && !/^[a-zA-Z0-9][a-zA-Z0-9\-.+]*$/.test(version)) {
|
if (version !== undefined && !/^[a-zA-Z0-9][a-zA-Z0-9\-.+]*$/.test(version)) {
|
||||||
throw new RequestError(`Invalid plugin version: ${version}`);
|
throw new ValidationError(`Invalid plugin version: ${version}`);
|
||||||
}
|
}
|
||||||
return { name, version };
|
return { name, version };
|
||||||
}
|
}
|
||||||
@@ -37,7 +37,7 @@ export async function checkRegistry(name: string, version?: string) {
|
|||||||
signal: AbortSignal.timeout(registryTimeoutMs),
|
signal: AbortSignal.timeout(registryTimeoutMs),
|
||||||
});
|
});
|
||||||
if (!npmRequest.ok) {
|
if (!npmRequest.ok) {
|
||||||
throw new RequestError(`Unknown plugin: ${spec}`);
|
throw new NotFoundError(`Unknown plugin: ${spec}`);
|
||||||
}
|
}
|
||||||
return spec;
|
return spec;
|
||||||
}
|
}
|
||||||
@@ -55,12 +55,12 @@ export type LatestVersionCache = Map<string, { version?: string, fetchedAt: numb
|
|||||||
*/
|
*/
|
||||||
export async function fetchLatestVersions(packages: string[], cache: LatestVersionCache) {
|
export async function fetchLatestVersions(packages: string[], cache: LatestVersionCache) {
|
||||||
if (packages.length > 500) {
|
if (packages.length > 500) {
|
||||||
throw new RequestError("Too many packages");
|
throw new ValidationError("Too many packages");
|
||||||
}
|
}
|
||||||
const names = [...new Set(packages.map(packageName => {
|
const names = [...new Set(packages.map(packageName => {
|
||||||
const { name, version: pinned } = parsePackageSpec(packageName);
|
const { name, version: pinned } = parsePackageSpec(packageName);
|
||||||
if (pinned !== undefined) {
|
if (pinned !== undefined) {
|
||||||
throw new RequestError(`Invalid plugin name: ${packageName}`);
|
throw new ValidationError(`Invalid plugin name: ${packageName}`);
|
||||||
}
|
}
|
||||||
return name;
|
return name;
|
||||||
}))];
|
}))];
|
||||||
@@ -117,10 +117,10 @@ const npmSearchMaxSize = 250; // Limit imposed by the registry
|
|||||||
|
|
||||||
export async function searchPlugins(query: string, page: number, pageSize: number) {
|
export async function searchPlugins(query: string, page: number, pageSize: number) {
|
||||||
if (query.length > 214) {
|
if (query.length > 214) {
|
||||||
throw new RequestError("Search query too long");
|
throw new ValidationError("Search query too long");
|
||||||
}
|
}
|
||||||
if (page < 1 || pageSize < 1 || pageSize > npmSearchMaxSize) {
|
if (page < 1 || pageSize < 1 || pageSize > npmSearchMaxSize) {
|
||||||
throw new RequestError("Invalid page or page size");
|
throw new ValidationError("Invalid page or page size");
|
||||||
}
|
}
|
||||||
|
|
||||||
const url = new URL("https://registry.npmjs.com/-/v1/search");
|
const url = new URL("https://registry.npmjs.com/-/v1/search");
|
||||||
@@ -130,7 +130,7 @@ export async function searchPlugins(query: string, page: number, pageSize: numbe
|
|||||||
|
|
||||||
const response = await fetch(url, { signal: AbortSignal.timeout(registryTimeoutMs) });
|
const response = await fetch(url, { signal: AbortSignal.timeout(registryTimeoutMs) });
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new RequestError(`npm registry search failed: ${response.status} ${response.statusText}`);
|
throw new UnavailableError(`npm registry search failed: ${response.status} ${response.statusText}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const json = await response.json() as {
|
const json = await response.json() as {
|
||||||
|
|||||||
@@ -109,7 +109,7 @@ export default async function(context: ControllerPluginContext) {
|
|||||||
let acquisitionRecord = getAcquisition(playerName);
|
let acquisitionRecord = getAcquisition(playerName);
|
||||||
if (acquisitionRecord) {
|
if (acquisitionRecord) {
|
||||||
let instanceName = controller.instances.get(acquisitionRecord.instanceId)!.config.get("instance.name");
|
let instanceName = controller.instances.get(acquisitionRecord.instanceId)!.config.get("instance.name");
|
||||||
throw new lib.RequestError(
|
throw new lib.ConflictError(
|
||||||
`${playerName} is currently acquired by ${instanceName}, ` +
|
`${playerName} is currently acquired by ${instanceName}, ` +
|
||||||
"have the player leave or release the lock first"
|
"have the player leave or release the lock first"
|
||||||
);
|
);
|
||||||
@@ -340,7 +340,7 @@ export default async function(context: ControllerPluginContext) {
|
|||||||
controller.handle(msg.DeletePlayerDataRequest, async (request: msg.DeletePlayerDataRequest) => {
|
controller.handle(msg.DeletePlayerDataRequest, async (request: msg.DeletePlayerDataRequest) => {
|
||||||
let { playerName } = request;
|
let { playerName } = request;
|
||||||
if (!playerDatastore.has(playerName)) {
|
if (!playerDatastore.has(playerName)) {
|
||||||
throw new lib.RequestError(`No player data stored for ${playerName}`);
|
throw new lib.NotFoundError(`No player data stored for ${playerName}`);
|
||||||
}
|
}
|
||||||
checkNotAcquired(playerName);
|
checkNotAcquired(playerName);
|
||||||
|
|
||||||
@@ -354,7 +354,7 @@ export default async function(context: ControllerPluginContext) {
|
|||||||
controller.handle(msg.ForceReleaseRequest, async (request: msg.ForceReleaseRequest) => {
|
controller.handle(msg.ForceReleaseRequest, async (request: msg.ForceReleaseRequest) => {
|
||||||
let { playerName } = request;
|
let { playerName } = request;
|
||||||
if (!acquiredPlayers.delete(playerName)) {
|
if (!acquiredPlayers.delete(playerName)) {
|
||||||
throw new lib.RequestError(`${playerName} is not acquired by any instance`);
|
throw new lib.ConflictError(`${playerName} is not acquired by any instance`);
|
||||||
}
|
}
|
||||||
logger.info(`Released lock on ${playerName}`);
|
logger.info(`Released lock on ${playerName}`);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -172,7 +172,7 @@ describe("player_sync controller", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const acquiredError = new lib.RequestError(
|
const acquiredError = new lib.ConflictError(
|
||||||
"stored is currently acquired by Running, have the player leave or release the lock first"
|
"stored is currently acquired by Running, have the player leave or release the lock first"
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -323,7 +323,7 @@ describe("player_sync controller", function() {
|
|||||||
assert.equal((await saved()).get("stored").generation, 4);
|
assert.equal((await saved()).get("stored").generation, 4);
|
||||||
});
|
});
|
||||||
it("should reject unknown players", async function() {
|
it("should reject unknown players", async function() {
|
||||||
await assert.rejects(del("unknown"), new lib.RequestError("No player data stored for unknown"));
|
await assert.rejects(del("unknown"), new lib.NotFoundError("No player data stored for unknown"));
|
||||||
});
|
});
|
||||||
it("should reject when the player is acquired", async function() {
|
it("should reject when the player is acquired", async function() {
|
||||||
await assert.rejects(del("stored"), acquiredError);
|
await assert.rejects(del("stored"), acquiredError);
|
||||||
@@ -371,7 +371,7 @@ describe("player_sync controller", function() {
|
|||||||
});
|
});
|
||||||
it("should reject players that are not acquired", async function() {
|
it("should reject players that are not acquired", async function() {
|
||||||
await assert.rejects(
|
await assert.rejects(
|
||||||
release("unknown"), new lib.RequestError("unknown is not acquired by any instance"),
|
release("unknown"), new lib.ConflictError("unknown is not acquired by any instance"),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in new issue
Block a user