nginx-proxy-manager/backend/lib/error.js
Tom Winnerl d50d5c7287 feat(nginx): serialised configure that tests, rolls back and repairs other hosts
Every write of a host config now goes through one configure() that runs
under a re-entrant lock (lib/nginx-lock.js), so concurrent saves can't test
or rename each other's files:

- the new config is written and checked with nginx -t; when nginx rejects
  this host, the previous file (and .err file) is put back and an
  NginxConfigError with structured details is thrown (scope host/global,
  file, line, humanised message and hint, raw nginx output)
- when another host's file breaks the test, that host is only blamed when it
  fails on its own (lib/nginx-blame.js). It is regenerated from its database
  row first and only moved to .err and marked offline when that fails too.
  A conflict with the change being saved fails the save instead. An error
  that names no file (eg. an unknown variable) is only a conflict when it
  goes away with every other host's config moved aside; otherwise it is the
  host's own error and, when its advanced config uses the variable or path,
  reads "Advanced configuration: ..."
- errors are humanised (lib/nginx-errors.js) and mapped to the advanced
  config of the host where possible (new begin/end markers in the templates)
- the API returns error.details; a middleware redacts details that name a
  host the user can't see
- bulkGenerateConfigs (certificates) and the default site go through the
  same path, a host nginx rejects is taken offline instead of failing all
- nginx -s reload only signals the master, which reads the config later.
  reloadNginx() waits (bounded, 5 s) until the master runs a new generation
  of workers and the old ones are shutting down (lib/nginx-reload.js, from
  /proc) before the lock is released,
  otherwise the next holder's rejected candidate file could be what the
  master reads and the change saved before would never be applied. A reload
  that can't be confirmed is repeated once at the end of the locked section,
  after every rollback. When nginx logs an [emerg] for the reload (eg. a
  listen port in use, which nginx -t doesn't catch) the wait ends as soon
  as nginx gives up on it (for a bind() after its own retries), and after
  a repeated reload that isn't confirmed either, later saves don't repeat
  it until a reload is confirmed again, so they don't each wait 10 s
- reconcile() regenerates missing / moved-aside configs and retries hosts
  whose upstream didn't resolve yet; the startup commit runs it from
  backend/index.js (NPM_RECONCILE_ON_START)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 13:47:08 +01:00

130 lines
3.4 KiB
JavaScript

import _ from "lodash";
const errs = {
PermissionError: function (_, previous) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.previous = previous;
this.message = "Permission Denied";
this.public = true;
this.status = 403;
},
ItemNotFoundError: function (id, previous) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.previous = previous;
this.message = "Not Found";
if (id) {
this.message = `Not Found - ${id}`;
}
this.public = true;
this.status = 404;
},
TokenRevokedError: function (message, previous) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.previous = previous;
this.message = message;
this.public = true;
this.status = 401;
},
AuthError: function (message, messageI18n, previous) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.previous = previous;
this.message = message;
this.message_i18n = messageI18n;
this.public = true;
this.status = 400;
},
InternalError: function (message, previous) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.previous = previous;
this.message = message;
this.status = 500;
this.public = false;
},
InternalValidationError: function (message, previous) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.previous = previous;
this.message = message;
this.status = 400;
this.public = false;
},
ConfigurationError: function (message, previous) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.previous = previous;
this.message = message;
this.status = 400;
this.public = true;
},
CacheError: function (message, previous) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = message;
this.previous = previous;
this.status = 500;
this.public = false;
},
ValidationError: function (message, previous) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.previous = previous;
this.message = message;
this.public = true;
this.status = 400;
},
AssertionFailedError: function (message, previous) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.previous = previous;
this.message = message;
this.public = false;
this.status = 400;
},
/**
* Nginx rejected a generated configuration. `details` is a structured,
* user-safe description of the failure that is returned to the client.
*
* @param {String} message Human readable message
* @param {Object} details See docs/src/advanced-config/locations.md
* @param {Error} [previous]
*/
NginxConfigError: function (message, details, previous) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.previous = previous;
this.message = message;
this.details = details || {};
this.public = true;
this.status = 400;
},
CommandError: function (stdErr, code, previous) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.previous = previous;
this.message = stdErr;
this.code = code;
this.public = false;
},
};
_.forEach(errs, (err) => {
err.prototype = Object.create(Error.prototype);
});
export default errs;