mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-16 22:21:51 +00:00
Handle plugin load errors gracefully
This commit is contained in:
parent
fcc3c17b73
commit
557898e484
4 changed files with 38 additions and 18 deletions
|
@ -76,10 +76,9 @@ export function initGuildsAPI(app: express.Express) {
|
||||||
parsedConfig = {};
|
parsedConfig = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
const errors = await validateGuildConfig(parsedConfig);
|
const error = await validateGuildConfig(parsedConfig);
|
||||||
|
if (error) {
|
||||||
if (errors) {
|
return res.status(422).json({ errors: [error] });
|
||||||
return res.status(422).json({ errors });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await configs.saveNewRevision(`guild-${req.params.guildId}`, config, req.user.userId);
|
await configs.saveNewRevision(`guild-${req.params.guildId}`, config, req.user.userId);
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { guildPlugins } from "./plugins/availablePlugins";
|
||||||
import { decodeAndValidateStrict, StrictValidationError } from "./validatorUtils";
|
import { decodeAndValidateStrict, StrictValidationError } from "./validatorUtils";
|
||||||
import { ZeppelinPlugin } from "./plugins/ZeppelinPlugin";
|
import { ZeppelinPlugin } from "./plugins/ZeppelinPlugin";
|
||||||
import { IZeppelinGuildConfig } from "./types";
|
import { IZeppelinGuildConfig } from "./types";
|
||||||
import { configUtils, PluginOptions } from "knub";
|
import { configUtils, ConfigValidationError, PluginOptions } from "knub";
|
||||||
|
|
||||||
const pluginNameToPlugin = new Map<string, ZeppelinPlugin>();
|
const pluginNameToPlugin = new Map<string, ZeppelinPlugin>();
|
||||||
for (const plugin of guildPlugins) {
|
for (const plugin of guildPlugins) {
|
||||||
|
@ -26,7 +26,7 @@ const globalConfigRootSchema = t.type({
|
||||||
|
|
||||||
const partialMegaTest = t.partial({ name: t.string });
|
const partialMegaTest = t.partial({ name: t.string });
|
||||||
|
|
||||||
export async function validateGuildConfig(config: any): Promise<string[] | null> {
|
export async function validateGuildConfig(config: any): Promise<string | null> {
|
||||||
const validationResult = decodeAndValidateStrict(partialGuildConfigRootSchema, config);
|
const validationResult = decodeAndValidateStrict(partialGuildConfigRootSchema, config);
|
||||||
if (validationResult instanceof StrictValidationError) return validationResult.getErrors();
|
if (validationResult instanceof StrictValidationError) return validationResult.getErrors();
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ export async function validateGuildConfig(config: any): Promise<string[] | null>
|
||||||
if (guildConfig.plugins) {
|
if (guildConfig.plugins) {
|
||||||
for (const [pluginName, pluginOptions] of Object.entries(guildConfig.plugins)) {
|
for (const [pluginName, pluginOptions] of Object.entries(guildConfig.plugins)) {
|
||||||
if (!pluginNameToPlugin.has(pluginName)) {
|
if (!pluginNameToPlugin.has(pluginName)) {
|
||||||
return [`Unknown plugin: ${pluginName}`];
|
return `Unknown plugin: ${pluginName}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const plugin = pluginNameToPlugin.get(pluginName);
|
const plugin = pluginNameToPlugin.get(pluginName);
|
||||||
|
@ -43,10 +43,8 @@ export async function validateGuildConfig(config: any): Promise<string[] | null>
|
||||||
const mergedOptions = configUtils.mergeConfig(plugin.defaultOptions || {}, pluginOptions);
|
const mergedOptions = configUtils.mergeConfig(plugin.defaultOptions || {}, pluginOptions);
|
||||||
await plugin.configPreprocessor(mergedOptions as PluginOptions<any>);
|
await plugin.configPreprocessor(mergedOptions as PluginOptions<any>);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof StrictValidationError) {
|
if (err instanceof ConfigValidationError) {
|
||||||
return err.getErrors().map(err => {
|
return `${pluginName}: ${err.toString()}`;
|
||||||
return `${pluginName}: ${err.toString()}`;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw err;
|
throw err;
|
||||||
|
|
|
@ -21,6 +21,7 @@ import { GuildLogs } from "./data/GuildLogs";
|
||||||
import { LogType } from "./data/LogType";
|
import { LogType } from "./data/LogType";
|
||||||
import { ZeppelinPlugin } from "./plugins/ZeppelinPlugin";
|
import { ZeppelinPlugin } from "./plugins/ZeppelinPlugin";
|
||||||
import { logger } from "./logger";
|
import { logger } from "./logger";
|
||||||
|
import { PluginLoadError } from "knub/dist/plugins/PluginLoadError";
|
||||||
|
|
||||||
const fsp = fs.promises;
|
const fsp = fs.promises;
|
||||||
|
|
||||||
|
@ -59,6 +60,12 @@ if (process.env.NODE_ENV === "production") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (err instanceof PluginLoadError) {
|
||||||
|
// tslint:disable:no-console
|
||||||
|
console.warn(`${err.guild.name} (${err.guild.id}): Failed to load plugin '${err.pluginName}': ${err.message}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// tslint:disable:no-console
|
// tslint:disable:no-console
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,15 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Member } from "eris";
|
import { Member } from "eris";
|
||||||
import { CommandContext, configUtils, helpers, PluginBlueprint, PluginData, PluginOptions } from "knub";
|
import {
|
||||||
|
CommandContext,
|
||||||
|
configUtils,
|
||||||
|
ConfigValidationError,
|
||||||
|
helpers,
|
||||||
|
PluginBlueprint,
|
||||||
|
PluginData,
|
||||||
|
PluginOptions,
|
||||||
|
} from "knub";
|
||||||
import { decodeAndValidateStrict, StrictValidationError, validate } from "./validatorUtils";
|
import { decodeAndValidateStrict, StrictValidationError, validate } from "./validatorUtils";
|
||||||
import { deepKeyIntersect, errorMessage, successMessage, tDeepPartial, tNullable } from "./utils";
|
import { deepKeyIntersect, errorMessage, successMessage, tDeepPartial, tNullable } from "./utils";
|
||||||
import { ZeppelinPluginBlueprint } from "./plugins/ZeppelinPluginBlueprint";
|
import { ZeppelinPluginBlueprint } from "./plugins/ZeppelinPluginBlueprint";
|
||||||
|
@ -52,6 +60,15 @@ const BasicPluginStructureType = t.type({
|
||||||
replaceDefaultOverrides: tNullable(t.boolean),
|
replaceDefaultOverrides: tNullable(t.boolean),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export function strictValidationErrorToConfigValidationError(err: StrictValidationError) {
|
||||||
|
return new ConfigValidationError(
|
||||||
|
err
|
||||||
|
.getErrors()
|
||||||
|
.map(e => e.toString())
|
||||||
|
.join("\n"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export function getPluginConfigPreprocessor(
|
export function getPluginConfigPreprocessor(
|
||||||
blueprint: ZeppelinPluginBlueprint,
|
blueprint: ZeppelinPluginBlueprint,
|
||||||
customPreprocessor?: PluginBlueprint<any>["configPreprocessor"],
|
customPreprocessor?: PluginBlueprint<any>["configPreprocessor"],
|
||||||
|
@ -60,17 +77,16 @@ export function getPluginConfigPreprocessor(
|
||||||
// 1. Validate the basic structure of plugin config
|
// 1. Validate the basic structure of plugin config
|
||||||
const basicOptionsValidation = validate(BasicPluginStructureType, options);
|
const basicOptionsValidation = validate(BasicPluginStructureType, options);
|
||||||
if (basicOptionsValidation instanceof StrictValidationError) {
|
if (basicOptionsValidation instanceof StrictValidationError) {
|
||||||
throw basicOptionsValidation;
|
throw strictValidationErrorToConfigValidationError(basicOptionsValidation);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Validate config/overrides against *partial* config schema. This ensures valid properties have valid types.
|
// 2. Validate config/overrides against *partial* config schema. This ensures valid properties have valid types.
|
||||||
const partialConfigSchema = tDeepPartial(blueprint.configSchema);
|
const partialConfigSchema = tDeepPartial(blueprint.configSchema);
|
||||||
// if (blueprint.name === "automod") console.log(partialConfigSchema);
|
|
||||||
|
|
||||||
if (options.config) {
|
if (options.config) {
|
||||||
const partialConfigValidation = validate(partialConfigSchema, options.config);
|
const partialConfigValidation = validate(partialConfigSchema, options.config);
|
||||||
if (partialConfigValidation instanceof StrictValidationError) {
|
if (partialConfigValidation instanceof StrictValidationError) {
|
||||||
throw partialConfigValidation;
|
throw strictValidationErrorToConfigValidationError(partialConfigValidation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,7 +94,7 @@ export function getPluginConfigPreprocessor(
|
||||||
for (const override of options.overrides) {
|
for (const override of options.overrides) {
|
||||||
const partialOverrideConfigValidation = validate(partialConfigSchema, override.config || {});
|
const partialOverrideConfigValidation = validate(partialConfigSchema, override.config || {});
|
||||||
if (partialOverrideConfigValidation) {
|
if (partialOverrideConfigValidation) {
|
||||||
throw partialOverrideConfigValidation;
|
throw strictValidationErrorToConfigValidationError(partialOverrideConfigValidation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -97,7 +113,7 @@ export function getPluginConfigPreprocessor(
|
||||||
? decodeAndValidateStrict(blueprint.configSchema, options.config)
|
? decodeAndValidateStrict(blueprint.configSchema, options.config)
|
||||||
: options.config;
|
: options.config;
|
||||||
if (decodedConfig instanceof StrictValidationError) {
|
if (decodedConfig instanceof StrictValidationError) {
|
||||||
throw decodedConfig;
|
throw strictValidationErrorToConfigValidationError(decodedConfig);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,7 +124,7 @@ export function getPluginConfigPreprocessor(
|
||||||
? decodeAndValidateStrict(blueprint.configSchema, overrideConfigMergedWithBaseConfig)
|
? decodeAndValidateStrict(blueprint.configSchema, overrideConfigMergedWithBaseConfig)
|
||||||
: overrideConfigMergedWithBaseConfig;
|
: overrideConfigMergedWithBaseConfig;
|
||||||
if (decodedOverrideConfig instanceof StrictValidationError) {
|
if (decodedOverrideConfig instanceof StrictValidationError) {
|
||||||
throw decodedOverrideConfig;
|
throw strictValidationErrorToConfigValidationError(decodedOverrideConfig);
|
||||||
}
|
}
|
||||||
decodedOverrides.push({
|
decodedOverrides.push({
|
||||||
...override,
|
...override,
|
||||||
|
|
Loading…
Add table
Reference in a new issue