@vandenberghinc/volt 1.1.13 → 1.1.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2056,7 +2056,7 @@ class UIDDocument {
2056
2056
  else {
2057
2057
  id += Object.values(this.path).join("@");
2058
2058
  }
2059
- throw new utils_js_1.APIError({
2059
+ throw new utils_js_1.InternalError({
2060
2060
  type: "DocumentNotFound",
2061
2061
  message: `Requested ${this.error_document_name} "${id}" does not exist.`,
2062
2062
  status: status_js_1.Status.bad_request,
@@ -2083,7 +2083,7 @@ class UIDDocument {
2083
2083
  else {
2084
2084
  id += Object.values(this.path).join("@");
2085
2085
  }
2086
- throw new utils_js_1.APIError({
2086
+ throw new utils_js_1.InternalError({
2087
2087
  type: "DocumentNotFound",
2088
2088
  message: `Document "${id}" is not loaded yet.`,
2089
2089
  status: status_js_1.Status.bad_request,
@@ -461,7 +461,7 @@ class Endpoint {
461
461
  }
462
462
  }
463
463
  catch (err) {
464
- if (err instanceof utils_js_1.APIError) {
464
+ if (err instanceof utils_js_1.ExternalError || err instanceof utils_js_1.InternalError) {
465
465
  err.serve(stream);
466
466
  }
467
467
  else {
@@ -624,7 +624,7 @@ class Paddle {
624
624
  }
625
625
  // Cancel.
626
626
  if (subscription.status !== "active") {
627
- throw new utils_js_1.APIError({
627
+ throw new utils_js_1.ExternalError({
628
628
  type: "NoActiveSubscriptionError",
629
629
  message: `This subscription is already cancelled and will become inactive at the end of the billing period.`,
630
630
  status: status_js_1.Status.bad_request,
@@ -1990,7 +1990,7 @@ class Paddle {
1990
1990
  }
1991
1991
  });
1992
1992
  if (_throw_no_cancelled_err && cancelled.length === 0) {
1993
- throw new utils_js_1.APIError({
1993
+ throw new utils_js_1.ExternalError({
1994
1994
  type: "NoCancellableSubscriptions",
1995
1995
  message: "No cancellable subscriptions found.",
1996
1996
  status: status_js_1.Status.bad_request,
@@ -974,14 +974,6 @@ class Server {
974
974
  // this.ts = ts as TypeScriptConfig;
975
975
  this.endpoints = new Map();
976
976
  this.err_endpoints = new Map();
977
- // Assign based on localhost.
978
- // if (localhost) {
979
- // this.ip = "127.0.0.1";
980
- // this.domain = this.ip + ":" + (port + (this.tls ? 1 : 0));
981
- // } else if (!production && !localhost && ip == null) { // use argument ip not attr since that already has a default.
982
- // this.ip = vlib.Network.private_ip();
983
- // this.domain = this.ip + ":" + (port + (this.tls ? 1 : 0));
984
- // }
985
977
  /* @performance */ this.performance = new _vinc_1.vlib.Performance("Server performance");
986
978
  // Assign objects to server so it is easy to access.
987
979
  this.status = status_js_1.Status;
@@ -44,7 +44,7 @@ const _vinc_1 = require("./vinc.js");
44
44
  const utils = __importStar(require("./utils.js"));
45
45
  const Mail = __importStar(require("./plugins/mail.js"));
46
46
  const status_js_1 = require("./status.js");
47
- const { APIError } = utils;
47
+ const { ExternalError } = utils;
48
48
  const logger_js_1 = require("./logger.js");
49
49
  const log_source = logger_js_1.logger.LogSource("Users");
50
50
  // interface Server {
@@ -519,7 +519,7 @@ class Users {
519
519
  }
520
520
  // Verify username and email.
521
521
  if (await this.username_exists(params.username)) {
522
- throw new APIError({
522
+ throw new ExternalError({
523
523
  type: "UsernameAlreadyExists",
524
524
  message: `Username "${params.username}" is already registered.`,
525
525
  status: status_js_1.Status.bad_request,
@@ -527,7 +527,7 @@ class Users {
527
527
  });
528
528
  }
529
529
  if (await this.email_exists(params.email)) {
530
- throw new APIError({
530
+ throw new ExternalError({
531
531
  type: "EmailAlreadyExists",
532
532
  message: `Email "${params.email}" is already registered.`,
533
533
  status: status_js_1.Status.bad_request,
@@ -1151,7 +1151,7 @@ class Users {
1151
1151
  // Check if username & email already exist.
1152
1152
  if (_check_username_email) {
1153
1153
  if (await this.username_exists(username)) {
1154
- throw new APIError({
1154
+ throw new ExternalError({
1155
1155
  type: "UsernameAlreadyExists",
1156
1156
  message: `Username "${username}" is already registered.`,
1157
1157
  status: status_js_1.Status.bad_request,
@@ -1159,7 +1159,7 @@ class Users {
1159
1159
  });
1160
1160
  }
1161
1161
  if (await this.email_exists(email)) {
1162
- throw new APIError({
1162
+ throw new ExternalError({
1163
1163
  type: "EmailAlreadyExists",
1164
1164
  message: `Email "${email}" is already registered.`,
1165
1165
  status: status_js_1.Status.bad_request,
@@ -1,8 +1,8 @@
1
1
  import type { Stream } from "./stream.js";
2
2
  /**
3
- * The API error class is used to throw an error that will be presented to the user. All other errors will result in an internal server error response without the error's message.
3
+ * The base class for internal and external errors.
4
4
  */
5
- export declare class APIError extends Error {
5
+ declare class BaseError extends Error {
6
6
  type: string;
7
7
  status: number;
8
8
  data?: any[] | Record<string, any>;
@@ -16,8 +16,22 @@ export declare class APIError extends Error {
16
16
  });
17
17
  serve(stream: Stream): this;
18
18
  }
19
+ /**
20
+ * Thrown external errors are presented to the user.
21
+ */
22
+ export declare class ExternalError extends BaseError {
23
+ constructor(args: ConstructorParameters<typeof BaseError>[0]);
24
+ serve(stream: Stream): this;
25
+ }
26
+ /**
27
+ * Thrown internal errors are not presented to the user, isntead an internal server error message is shown.
28
+ */
29
+ export declare class InternalError extends BaseError {
30
+ constructor(args: ConstructorParameters<typeof BaseError>[0]);
31
+ serve(stream: Stream): this;
32
+ }
19
33
  interface UtilsInt {
20
- "APIError": typeof APIError;
34
+ "APIError": typeof ExternalError;
21
35
  clean_endpoint(endpoint: undefined): undefined;
22
36
  clean_endpoint(endpoint: RegExp): RegExp;
23
37
  clean_endpoint(endpoint: string): string;
@@ -4,15 +4,15 @@
4
4
  * Copyright: © 2022 - 2024 Daan van den Bergh.
5
5
  */
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.utils = exports.Utils = exports.APIError = void 0;
7
+ exports.utils = exports.Utils = exports.InternalError = exports.ExternalError = void 0;
8
8
  const _vinc_1 = require("./vinc.js");
9
9
  const status_js_1 = require("./status.js");
10
10
  // ---------------------------------------------------------
11
11
  // Utils.
12
12
  /**
13
- * The API error class is used to throw an error that will be presented to the user. All other errors will result in an internal server error response without the error's message.
13
+ * The base class for internal and external errors.
14
14
  */
15
- class APIError extends Error {
15
+ class BaseError extends Error {
16
16
  type;
17
17
  status;
18
18
  data;
@@ -36,11 +36,49 @@ class APIError extends Error {
36
36
  return this;
37
37
  }
38
38
  }
39
- exports.APIError = APIError;
39
+ /**
40
+ * Thrown external errors are presented to the user.
41
+ */
42
+ class ExternalError extends BaseError {
43
+ constructor(args) {
44
+ args.type ??= "ExternalError";
45
+ super(args);
46
+ }
47
+ serve(stream) {
48
+ stream.error({
49
+ status: this.status ?? status_js_1.Status.internal_server_error,
50
+ headers: { "Content-Type": "application/json" },
51
+ message: this.message,
52
+ type: this.type,
53
+ invalid_fields: this.invalid_fields,
54
+ });
55
+ return this;
56
+ }
57
+ }
58
+ exports.ExternalError = ExternalError;
59
+ /**
60
+ * Thrown internal errors are not presented to the user, isntead an internal server error message is shown.
61
+ */
62
+ class InternalError extends BaseError {
63
+ constructor(args) {
64
+ args.type ??= "InternalError";
65
+ super(args);
66
+ }
67
+ serve(stream) {
68
+ stream.error({
69
+ status: status_js_1.Status.internal_server_error,
70
+ headers: { "Content-Type": "application/json" },
71
+ message: "Internal Server Error",
72
+ type: "InternalServerError",
73
+ });
74
+ return this;
75
+ }
76
+ }
77
+ exports.InternalError = InternalError;
40
78
  // Implementation
41
79
  exports.Utils = {
42
80
  // An error that may be shown to the frontend user.
43
- "APIError": APIError,
81
+ "APIError": ExternalError,
44
82
  // Clean an endpoint url.
45
83
  clean_endpoint(endpoint) {
46
84
  if (endpoint == null || endpoint instanceof RegExp) {
@@ -1,4 +1,4 @@
1
- export { APIError } from "./utils.js";
1
+ export { ExternalError as APIError } from "./utils.js";
2
2
  export * from "./status.js";
3
3
  export * from "./meta.js";
4
4
  export * from "./splash_screen.js";
@@ -45,7 +45,7 @@ exports.TypeScript = exports.Mail = exports.APIError = void 0;
45
45
  // Exports.
46
46
  // Create volt lib.
47
47
  var utils_js_1 = require("./utils.js");
48
- Object.defineProperty(exports, "APIError", { enumerable: true, get: function () { return utils_js_1.APIError; } });
48
+ Object.defineProperty(exports, "APIError", { enumerable: true, get: function () { return utils_js_1.ExternalError; } });
49
49
  __exportStar(require("./status.js"), exports);
50
50
  __exportStar(require("./meta.js"), exports);
51
51
  __exportStar(require("./splash_screen.js"), exports);
@@ -10,7 +10,7 @@ import { MongoClient, ServerApiVersion } from 'mongodb';
10
10
  import { logger, LogSource } from "./logger.js";
11
11
  import { Status } from "./status.js";
12
12
  import { vlib } from "./vinc.js";
13
- import { APIError } from "./utils.js";
13
+ import { InternalError } from "./utils.js";
14
14
  const log_source = new LogSource("Database");
15
15
  // ---------------------------------------------------------
16
16
  // Collection.
@@ -2050,7 +2050,7 @@ export class UIDDocument {
2050
2050
  else {
2051
2051
  id += Object.values(this.path).join("@");
2052
2052
  }
2053
- throw new APIError({
2053
+ throw new InternalError({
2054
2054
  type: "DocumentNotFound",
2055
2055
  message: `Requested ${this.error_document_name} "${id}" does not exist.`,
2056
2056
  status: Status.bad_request,
@@ -2077,7 +2077,7 @@ export class UIDDocument {
2077
2077
  else {
2078
2078
  id += Object.values(this.path).join("@");
2079
2079
  }
2080
- throw new APIError({
2080
+ throw new InternalError({
2081
2081
  type: "DocumentNotFound",
2082
2082
  message: `Document "${id}" is not loaded yet.`,
2083
2083
  status: Status.bad_request,
@@ -8,7 +8,7 @@ import CleanCSS from 'clean-css';
8
8
  import zlib from 'zlib';
9
9
  import { View } from './view.js';
10
10
  import { vlib } from "./vinc.js";
11
- import { Utils, APIError } from "./utils.js";
11
+ import { Utils, ExternalError, InternalError } from "./utils.js";
12
12
  import { Status } from "./status.js";
13
13
  import { logger, LogSource } from "./logger.js";
14
14
  import { RateLimits } from "./rate_limit.js";
@@ -455,7 +455,7 @@ class Endpoint {
455
455
  }
456
456
  }
457
457
  catch (err) {
458
- if (err instanceof APIError) {
458
+ if (err instanceof ExternalError || err instanceof InternalError) {
459
459
  err.serve(stream);
460
460
  }
461
461
  else {
@@ -8,7 +8,7 @@ import * as PDFDocument from "pdfkit";
8
8
  import * as libcrypto from "crypto";
9
9
  import blobstream from 'blob-stream';
10
10
  import { vlib } from "../vinc.js";
11
- import { Utils, APIError } from "../utils.js";
11
+ import { Utils, ExternalError } from "../utils.js";
12
12
  import { logger } from "../logger.js";
13
13
  import { Status } from "../status.js";
14
14
  const log_source = logger.LogSource("Payments");
@@ -585,7 +585,7 @@ export class Paddle {
585
585
  }
586
586
  // Cancel.
587
587
  if (subscription.status !== "active") {
588
- throw new APIError({
588
+ throw new ExternalError({
589
589
  type: "NoActiveSubscriptionError",
590
590
  message: `This subscription is already cancelled and will become inactive at the end of the billing period.`,
591
591
  status: Status.bad_request,
@@ -1951,7 +1951,7 @@ export class Paddle {
1951
1951
  }
1952
1952
  });
1953
1953
  if (_throw_no_cancelled_err && cancelled.length === 0) {
1954
- throw new APIError({
1954
+ throw new ExternalError({
1955
1955
  type: "NoCancellableSubscriptions",
1956
1956
  message: "No cancellable subscriptions found.",
1957
1957
  status: Status.bad_request,
@@ -935,14 +935,6 @@ export class Server {
935
935
  // this.ts = ts as TypeScriptConfig;
936
936
  this.endpoints = new Map();
937
937
  this.err_endpoints = new Map();
938
- // Assign based on localhost.
939
- // if (localhost) {
940
- // this.ip = "127.0.0.1";
941
- // this.domain = this.ip + ":" + (port + (this.tls ? 1 : 0));
942
- // } else if (!production && !localhost && ip == null) { // use argument ip not attr since that already has a default.
943
- // this.ip = vlib.Network.private_ip();
944
- // this.domain = this.ip + ":" + (port + (this.tls ? 1 : 0));
945
- // }
946
938
  /* @performance */ this.performance = new vlib.Performance("Server performance");
947
939
  // Assign objects to server so it is easy to access.
948
940
  this.status = Status;
@@ -8,7 +8,7 @@ import { vlib } from "./vinc.js";
8
8
  import * as utils from "./utils.js";
9
9
  import * as Mail from "./plugins/mail.js";
10
10
  import { Status } from "./status.js";
11
- const { APIError } = utils;
11
+ const { ExternalError } = utils;
12
12
  import { logger } from "./logger.js";
13
13
  const log_source = logger.LogSource("Users");
14
14
  // interface Server {
@@ -483,7 +483,7 @@ export class Users {
483
483
  }
484
484
  // Verify username and email.
485
485
  if (await this.username_exists(params.username)) {
486
- throw new APIError({
486
+ throw new ExternalError({
487
487
  type: "UsernameAlreadyExists",
488
488
  message: `Username "${params.username}" is already registered.`,
489
489
  status: Status.bad_request,
@@ -491,7 +491,7 @@ export class Users {
491
491
  });
492
492
  }
493
493
  if (await this.email_exists(params.email)) {
494
- throw new APIError({
494
+ throw new ExternalError({
495
495
  type: "EmailAlreadyExists",
496
496
  message: `Email "${params.email}" is already registered.`,
497
497
  status: Status.bad_request,
@@ -1115,7 +1115,7 @@ export class Users {
1115
1115
  // Check if username & email already exist.
1116
1116
  if (_check_username_email) {
1117
1117
  if (await this.username_exists(username)) {
1118
- throw new APIError({
1118
+ throw new ExternalError({
1119
1119
  type: "UsernameAlreadyExists",
1120
1120
  message: `Username "${username}" is already registered.`,
1121
1121
  status: Status.bad_request,
@@ -1123,7 +1123,7 @@ export class Users {
1123
1123
  });
1124
1124
  }
1125
1125
  if (await this.email_exists(email)) {
1126
- throw new APIError({
1126
+ throw new ExternalError({
1127
1127
  type: "EmailAlreadyExists",
1128
1128
  message: `Email "${email}" is already registered.`,
1129
1129
  status: Status.bad_request,
@@ -1,8 +1,8 @@
1
1
  import type { Stream } from "./stream.js";
2
2
  /**
3
- * The API error class is used to throw an error that will be presented to the user. All other errors will result in an internal server error response without the error's message.
3
+ * The base class for internal and external errors.
4
4
  */
5
- export declare class APIError extends Error {
5
+ declare class BaseError extends Error {
6
6
  type: string;
7
7
  status: number;
8
8
  data?: any[] | Record<string, any>;
@@ -16,8 +16,22 @@ export declare class APIError extends Error {
16
16
  });
17
17
  serve(stream: Stream): this;
18
18
  }
19
+ /**
20
+ * Thrown external errors are presented to the user.
21
+ */
22
+ export declare class ExternalError extends BaseError {
23
+ constructor(args: ConstructorParameters<typeof BaseError>[0]);
24
+ serve(stream: Stream): this;
25
+ }
26
+ /**
27
+ * Thrown internal errors are not presented to the user, isntead an internal server error message is shown.
28
+ */
29
+ export declare class InternalError extends BaseError {
30
+ constructor(args: ConstructorParameters<typeof BaseError>[0]);
31
+ serve(stream: Stream): this;
32
+ }
19
33
  interface UtilsInt {
20
- "APIError": typeof APIError;
34
+ "APIError": typeof ExternalError;
21
35
  clean_endpoint(endpoint: undefined): undefined;
22
36
  clean_endpoint(endpoint: RegExp): RegExp;
23
37
  clean_endpoint(endpoint: string): string;
@@ -7,9 +7,9 @@ import { Status } from "./status.js";
7
7
  // ---------------------------------------------------------
8
8
  // Utils.
9
9
  /**
10
- * The API error class is used to throw an error that will be presented to the user. All other errors will result in an internal server error response without the error's message.
10
+ * The base class for internal and external errors.
11
11
  */
12
- export class APIError extends Error {
12
+ class BaseError extends Error {
13
13
  type;
14
14
  status;
15
15
  data;
@@ -33,10 +33,47 @@ export class APIError extends Error {
33
33
  return this;
34
34
  }
35
35
  }
36
+ /**
37
+ * Thrown external errors are presented to the user.
38
+ */
39
+ export class ExternalError extends BaseError {
40
+ constructor(args) {
41
+ args.type ??= "ExternalError";
42
+ super(args);
43
+ }
44
+ serve(stream) {
45
+ stream.error({
46
+ status: this.status ?? Status.internal_server_error,
47
+ headers: { "Content-Type": "application/json" },
48
+ message: this.message,
49
+ type: this.type,
50
+ invalid_fields: this.invalid_fields,
51
+ });
52
+ return this;
53
+ }
54
+ }
55
+ /**
56
+ * Thrown internal errors are not presented to the user, isntead an internal server error message is shown.
57
+ */
58
+ export class InternalError extends BaseError {
59
+ constructor(args) {
60
+ args.type ??= "InternalError";
61
+ super(args);
62
+ }
63
+ serve(stream) {
64
+ stream.error({
65
+ status: Status.internal_server_error,
66
+ headers: { "Content-Type": "application/json" },
67
+ message: "Internal Server Error",
68
+ type: "InternalServerError",
69
+ });
70
+ return this;
71
+ }
72
+ }
36
73
  // Implementation
37
74
  export const Utils = {
38
75
  // An error that may be shown to the frontend user.
39
- "APIError": APIError,
76
+ "APIError": ExternalError,
40
77
  // Clean an endpoint url.
41
78
  clean_endpoint(endpoint) {
42
79
  if (endpoint == null || endpoint instanceof RegExp) {
@@ -1,4 +1,4 @@
1
- export { APIError } from "./utils.js";
1
+ export { ExternalError as APIError } from "./utils.js";
2
2
  export * from "./status.js";
3
3
  export * from "./meta.js";
4
4
  export * from "./splash_screen.js";
@@ -5,7 +5,7 @@
5
5
  // ---------------------------------------------------------
6
6
  // Exports.
7
7
  // Create volt lib.
8
- export { APIError } from "./utils.js";
8
+ export { ExternalError as APIError } from "./utils.js";
9
9
  export * from "./status.js";
10
10
  export * from "./meta.js";
11
11
  export * from "./splash_screen.js";
@@ -10,7 +10,7 @@ import { MongoClient, ServerApiVersion } from 'mongodb';
10
10
  import { logger, LogSource } from "./logger.js";
11
11
  import { Status } from "./status.js";
12
12
  import { vlib } from "@vinc";
13
- import { APIError } from "./utils.js";
13
+ import { InternalError } from "./utils.js";
14
14
  const log_source = new LogSource("Database");
15
15
  // ---------------------------------------------------------
16
16
  // Collection.
@@ -2050,7 +2050,7 @@ export class UIDDocument {
2050
2050
  else {
2051
2051
  id += Object.values(this.path).join("@");
2052
2052
  }
2053
- throw new APIError({
2053
+ throw new InternalError({
2054
2054
  type: "DocumentNotFound",
2055
2055
  message: `Requested ${this.error_document_name} "${id}" does not exist.`,
2056
2056
  status: Status.bad_request,
@@ -2077,7 +2077,7 @@ export class UIDDocument {
2077
2077
  else {
2078
2078
  id += Object.values(this.path).join("@");
2079
2079
  }
2080
- throw new APIError({
2080
+ throw new InternalError({
2081
2081
  type: "DocumentNotFound",
2082
2082
  message: `Document "${id}" is not loaded yet.`,
2083
2083
  status: Status.bad_request,
@@ -8,7 +8,7 @@ import CleanCSS from 'clean-css';
8
8
  import zlib from 'zlib';
9
9
  import { View } from './view.js';
10
10
  import { vlib } from "@vinc";
11
- import { Utils, APIError } from "./utils.js";
11
+ import { Utils, ExternalError, InternalError } from "./utils.js";
12
12
  import { Status } from "./status.js";
13
13
  import { logger, LogSource } from "./logger.js";
14
14
  import { RateLimits } from "./rate_limit.js";
@@ -455,7 +455,7 @@ class Endpoint {
455
455
  }
456
456
  }
457
457
  catch (err) {
458
- if (err instanceof APIError) {
458
+ if (err instanceof ExternalError || err instanceof InternalError) {
459
459
  err.serve(stream);
460
460
  }
461
461
  else {
@@ -8,7 +8,7 @@ import * as PDFDocument from "pdfkit";
8
8
  import * as libcrypto from "crypto";
9
9
  import blobstream from 'blob-stream';
10
10
  import { vlib } from "@vinc";
11
- import { Utils, APIError } from "../utils.js";
11
+ import { Utils, ExternalError } from "../utils.js";
12
12
  import { logger } from "../logger.js";
13
13
  import { Status } from "../status.js";
14
14
  const log_source = logger.LogSource("Payments");
@@ -585,7 +585,7 @@ export class Paddle {
585
585
  }
586
586
  // Cancel.
587
587
  if (subscription.status !== "active") {
588
- throw new APIError({
588
+ throw new ExternalError({
589
589
  type: "NoActiveSubscriptionError",
590
590
  message: `This subscription is already cancelled and will become inactive at the end of the billing period.`,
591
591
  status: Status.bad_request,
@@ -1951,7 +1951,7 @@ export class Paddle {
1951
1951
  }
1952
1952
  });
1953
1953
  if (_throw_no_cancelled_err && cancelled.length === 0) {
1954
- throw new APIError({
1954
+ throw new ExternalError({
1955
1955
  type: "NoCancellableSubscriptions",
1956
1956
  message: "No cancellable subscriptions found.",
1957
1957
  status: Status.bad_request,
@@ -935,14 +935,6 @@ export class Server {
935
935
  // this.ts = ts as TypeScriptConfig;
936
936
  this.endpoints = new Map();
937
937
  this.err_endpoints = new Map();
938
- // Assign based on localhost.
939
- // if (localhost) {
940
- // this.ip = "127.0.0.1";
941
- // this.domain = this.ip + ":" + (port + (this.tls ? 1 : 0));
942
- // } else if (!production && !localhost && ip == null) { // use argument ip not attr since that already has a default.
943
- // this.ip = vlib.Network.private_ip();
944
- // this.domain = this.ip + ":" + (port + (this.tls ? 1 : 0));
945
- // }
946
938
  /* @performance */ this.performance = new vlib.Performance("Server performance");
947
939
  // Assign objects to server so it is easy to access.
948
940
  this.status = Status;
@@ -8,7 +8,7 @@ import { vlib } from "@vinc";
8
8
  import * as utils from "./utils.js";
9
9
  import * as Mail from "./plugins/mail.js";
10
10
  import { Status } from "./status.js";
11
- const { APIError } = utils;
11
+ const { ExternalError } = utils;
12
12
  import { logger } from "./logger.js";
13
13
  const log_source = logger.LogSource("Users");
14
14
  // interface Server {
@@ -483,7 +483,7 @@ export class Users {
483
483
  }
484
484
  // Verify username and email.
485
485
  if (await this.username_exists(params.username)) {
486
- throw new APIError({
486
+ throw new ExternalError({
487
487
  type: "UsernameAlreadyExists",
488
488
  message: `Username "${params.username}" is already registered.`,
489
489
  status: Status.bad_request,
@@ -491,7 +491,7 @@ export class Users {
491
491
  });
492
492
  }
493
493
  if (await this.email_exists(params.email)) {
494
- throw new APIError({
494
+ throw new ExternalError({
495
495
  type: "EmailAlreadyExists",
496
496
  message: `Email "${params.email}" is already registered.`,
497
497
  status: Status.bad_request,
@@ -1115,7 +1115,7 @@ export class Users {
1115
1115
  // Check if username & email already exist.
1116
1116
  if (_check_username_email) {
1117
1117
  if (await this.username_exists(username)) {
1118
- throw new APIError({
1118
+ throw new ExternalError({
1119
1119
  type: "UsernameAlreadyExists",
1120
1120
  message: `Username "${username}" is already registered.`,
1121
1121
  status: Status.bad_request,
@@ -1123,7 +1123,7 @@ export class Users {
1123
1123
  });
1124
1124
  }
1125
1125
  if (await this.email_exists(email)) {
1126
- throw new APIError({
1126
+ throw new ExternalError({
1127
1127
  type: "EmailAlreadyExists",
1128
1128
  message: `Email "${email}" is already registered.`,
1129
1129
  status: Status.bad_request,
@@ -1,8 +1,8 @@
1
1
  import type { Stream } from "./stream.js";
2
2
  /**
3
- * The API error class is used to throw an error that will be presented to the user. All other errors will result in an internal server error response without the error's message.
3
+ * The base class for internal and external errors.
4
4
  */
5
- export declare class APIError extends Error {
5
+ declare class BaseError extends Error {
6
6
  type: string;
7
7
  status: number;
8
8
  data?: any[] | Record<string, any>;
@@ -16,8 +16,22 @@ export declare class APIError extends Error {
16
16
  });
17
17
  serve(stream: Stream): this;
18
18
  }
19
+ /**
20
+ * Thrown external errors are presented to the user.
21
+ */
22
+ export declare class ExternalError extends BaseError {
23
+ constructor(args: ConstructorParameters<typeof BaseError>[0]);
24
+ serve(stream: Stream): this;
25
+ }
26
+ /**
27
+ * Thrown internal errors are not presented to the user, isntead an internal server error message is shown.
28
+ */
29
+ export declare class InternalError extends BaseError {
30
+ constructor(args: ConstructorParameters<typeof BaseError>[0]);
31
+ serve(stream: Stream): this;
32
+ }
19
33
  interface UtilsInt {
20
- "APIError": typeof APIError;
34
+ "APIError": typeof ExternalError;
21
35
  clean_endpoint(endpoint: undefined): undefined;
22
36
  clean_endpoint(endpoint: RegExp): RegExp;
23
37
  clean_endpoint(endpoint: string): string;
@@ -7,9 +7,9 @@ import { Status } from "./status.js";
7
7
  // ---------------------------------------------------------
8
8
  // Utils.
9
9
  /**
10
- * The API error class is used to throw an error that will be presented to the user. All other errors will result in an internal server error response without the error's message.
10
+ * The base class for internal and external errors.
11
11
  */
12
- export class APIError extends Error {
12
+ class BaseError extends Error {
13
13
  type;
14
14
  status;
15
15
  data;
@@ -33,10 +33,47 @@ export class APIError extends Error {
33
33
  return this;
34
34
  }
35
35
  }
36
+ /**
37
+ * Thrown external errors are presented to the user.
38
+ */
39
+ export class ExternalError extends BaseError {
40
+ constructor(args) {
41
+ args.type ??= "ExternalError";
42
+ super(args);
43
+ }
44
+ serve(stream) {
45
+ stream.error({
46
+ status: this.status ?? Status.internal_server_error,
47
+ headers: { "Content-Type": "application/json" },
48
+ message: this.message,
49
+ type: this.type,
50
+ invalid_fields: this.invalid_fields,
51
+ });
52
+ return this;
53
+ }
54
+ }
55
+ /**
56
+ * Thrown internal errors are not presented to the user, isntead an internal server error message is shown.
57
+ */
58
+ export class InternalError extends BaseError {
59
+ constructor(args) {
60
+ args.type ??= "InternalError";
61
+ super(args);
62
+ }
63
+ serve(stream) {
64
+ stream.error({
65
+ status: Status.internal_server_error,
66
+ headers: { "Content-Type": "application/json" },
67
+ message: "Internal Server Error",
68
+ type: "InternalServerError",
69
+ });
70
+ return this;
71
+ }
72
+ }
36
73
  // Implementation
37
74
  export const Utils = {
38
75
  // An error that may be shown to the frontend user.
39
- "APIError": APIError,
76
+ "APIError": ExternalError,
40
77
  // Clean an endpoint url.
41
78
  clean_endpoint(endpoint) {
42
79
  if (endpoint == null || endpoint instanceof RegExp) {
@@ -1,4 +1,4 @@
1
- export { APIError } from "./utils.js";
1
+ export { ExternalError as APIError } from "./utils.js";
2
2
  export * from "./status.js";
3
3
  export * from "./meta.js";
4
4
  export * from "./splash_screen.js";
@@ -5,7 +5,7 @@
5
5
  // ---------------------------------------------------------
6
6
  // Exports.
7
7
  // Create volt lib.
8
- export { APIError } from "./utils.js";
8
+ export { ExternalError as APIError } from "./utils.js";
9
9
  export * from "./status.js";
10
10
  export * from "./meta.js";
11
11
  export * from "./splash_screen.js";
@@ -14,7 +14,7 @@ import { logger, LogSource } from "./logger.js";
14
14
  import { Status } from "./status.js";
15
15
  import { vlib } from "@vinc";
16
16
  import { resolve } from "path";
17
- import { APIError } from "./utils.js";
17
+ import { InternalError } from "./utils.js";
18
18
 
19
19
  const log_source = new LogSource("Database")
20
20
 
@@ -2311,7 +2311,7 @@ export class UIDDocument<DocumentOptions = any> {
2311
2311
  } else {
2312
2312
  id += Object.values(this.path).join("@");
2313
2313
  }
2314
- throw new APIError({
2314
+ throw new InternalError({
2315
2315
  type: "DocumentNotFound",
2316
2316
  message: `Requested ${this.error_document_name} "${id}" does not exist.`,
2317
2317
  status: Status.bad_request,
@@ -2342,7 +2342,7 @@ export class UIDDocument<DocumentOptions = any> {
2342
2342
  } else {
2343
2343
  id += Object.values(this.path).join("@");
2344
2344
  }
2345
- throw new APIError({
2345
+ throw new InternalError({
2346
2346
  type: "DocumentNotFound",
2347
2347
  message: `Document "${id}" is not loaded yet.`,
2348
2348
  status: Status.bad_request,
@@ -11,7 +11,7 @@ import zlib from 'zlib';
11
11
  import { View } from './view.js';
12
12
  import { vlib } from "@vinc";
13
13
  import { vhighlight } from "@vinc";
14
- import { Utils, APIError } from "./utils.js";
14
+ import { Utils, ExternalError, InternalError } from "./utils.js";
15
15
  import { Status } from "./status.js";
16
16
  import { logger, LogSource } from "./logger.js";
17
17
  import { RateLimits, RateLimitGroup } from "./rate_limit.js";
@@ -540,7 +540,7 @@ class Endpoint {
540
540
  await promise;
541
541
  }
542
542
  } catch (err: any) {
543
- if (err instanceof APIError) {
543
+ if (err instanceof ExternalError || err instanceof InternalError) {
544
544
  err.serve(stream);
545
545
  } else {
546
546
  stream.error({
@@ -12,7 +12,7 @@ import blobstream from 'blob-stream';
12
12
 
13
13
 
14
14
  import { vlib } from "@vinc";
15
- import { Utils, APIError } from "../utils.js";
15
+ import { Utils, ExternalError } from "../utils.js";
16
16
  import { logger } from "../logger.js";
17
17
  import { Status } from "../status.js";
18
18
 
@@ -732,7 +732,7 @@ export class Paddle {
732
732
 
733
733
  // Cancel.
734
734
  if (subscription.status !== "active") {
735
- throw new APIError({
735
+ throw new ExternalError({
736
736
  type: "NoActiveSubscriptionError",
737
737
  message: `This subscription is already cancelled and will become inactive at the end of the billing period.`,
738
738
  status: Status.bad_request,
@@ -2322,7 +2322,7 @@ export class Paddle {
2322
2322
  }
2323
2323
  });
2324
2324
  if (_throw_no_cancelled_err && cancelled.length === 0) {
2325
- throw new APIError({
2325
+ throw new ExternalError({
2326
2326
  type: "NoCancellableSubscriptions",
2327
2327
  message: "No cancellable subscriptions found.",
2328
2328
  status: Status.bad_request,
@@ -20,7 +20,7 @@ import CleanCSS from 'clean-css';
20
20
 
21
21
  import { vlib } from "@vinc";
22
22
  import { vhighlight } from "@vinc";
23
- import { Utils, APIError } from "./utils.js";
23
+ import { Utils, ExternalError } from "./utils.js";
24
24
  import { Meta } from './meta.js';
25
25
  import * as Mail from './plugins/mail.js';
26
26
  import { Status } from "./status.js";
@@ -1121,15 +1121,6 @@ export class Server {
1121
1121
  this.endpoints = new Map();
1122
1122
  this.err_endpoints = new Map();
1123
1123
 
1124
- // Assign based on localhost.
1125
- // if (localhost) {
1126
- // this.ip = "127.0.0.1";
1127
- // this.domain = this.ip + ":" + (port + (this.tls ? 1 : 0));
1128
- // } else if (!production && !localhost && ip == null) { // use argument ip not attr since that already has a default.
1129
- // this.ip = vlib.Network.private_ip();
1130
- // this.domain = this.ip + ":" + (port + (this.tls ? 1 : 0));
1131
- // }
1132
-
1133
1124
  /* @performance */ this.performance = new vlib.Performance("Server performance");
1134
1125
 
1135
1126
  // Assign objects to server so it is easy to access.
@@ -11,7 +11,7 @@ import { vhighlight } from "@vinc";
11
11
  import * as utils from "./utils.js";
12
12
  import * as Mail from "./plugins/mail.js";
13
13
  import { Status } from "./status.js";
14
- const { APIError } = utils;
14
+ const { ExternalError } = utils;
15
15
  import { logger } from "./logger.js";
16
16
  import { Stream, AuthStream } from "./stream.js"
17
17
  import { Server, MailAttachment } from "./server.js"
@@ -553,7 +553,7 @@ export class Users {
553
553
 
554
554
  // Verify username and email.
555
555
  if (await this.username_exists(params.username)) {
556
- throw new APIError({
556
+ throw new ExternalError({
557
557
  type: "UsernameAlreadyExists",
558
558
  message: `Username "${params.username}" is already registered.`,
559
559
  status: Status.bad_request,
@@ -561,7 +561,7 @@ export class Users {
561
561
  });
562
562
  }
563
563
  if (await this.email_exists(params.email)) {
564
- throw new APIError({
564
+ throw new ExternalError({
565
565
  type: "EmailAlreadyExists",
566
566
  message: `Email "${params.email}" is already registered.`,
567
567
  status: Status.bad_request,
@@ -1255,7 +1255,7 @@ export class Users {
1255
1255
  // Check if username & email already exist.
1256
1256
  if (_check_username_email) {
1257
1257
  if (await this.username_exists(username)) {
1258
- throw new APIError({
1258
+ throw new ExternalError({
1259
1259
  type: "UsernameAlreadyExists",
1260
1260
  message: `Username "${username}" is already registered.`,
1261
1261
  status: Status.bad_request,
@@ -1263,7 +1263,7 @@ export class Users {
1263
1263
  });
1264
1264
  }
1265
1265
  if (await this.email_exists(email)) {
1266
- throw new APIError({
1266
+ throw new ExternalError({
1267
1267
  type: "EmailAlreadyExists",
1268
1268
  message: `Email "${email}" is already registered.`,
1269
1269
  status: Status.bad_request,
@@ -15,9 +15,9 @@ import type { Stream } from "./stream.js";
15
15
  // Utils.
16
16
 
17
17
  /**
18
- * The API error class is used to throw an error that will be presented to the user. All other errors will result in an internal server error response without the error's message.
18
+ * The base class for internal and external errors.
19
19
  */
20
- export class APIError extends Error {
20
+ class BaseError extends Error {
21
21
  public type: string;
22
22
  public status: number;
23
23
  public data?: any[] | Record<string, any>;
@@ -48,6 +48,45 @@ export class APIError extends Error {
48
48
  }
49
49
  }
50
50
 
51
+ /**
52
+ * Thrown external errors are presented to the user.
53
+ */
54
+ export class ExternalError extends BaseError {
55
+ constructor(args: ConstructorParameters<typeof BaseError>[0]) {
56
+ args.type ??= "ExternalError";
57
+ super(args);
58
+ }
59
+ serve(stream: Stream) {
60
+ stream.error({
61
+ status: this.status ?? Status.internal_server_error,
62
+ headers: { "Content-Type": "application/json" },
63
+ message: this.message,
64
+ type: this.type,
65
+ invalid_fields: this.invalid_fields,
66
+ });
67
+ return this;
68
+ }
69
+ }
70
+
71
+ /**
72
+ * Thrown internal errors are not presented to the user, isntead an internal server error message is shown.
73
+ */
74
+ export class InternalError extends BaseError {
75
+ constructor(args: ConstructorParameters<typeof BaseError>[0]) {
76
+ args.type ??= "InternalError";
77
+ super(args);
78
+ }
79
+ serve(stream: Stream) {
80
+ stream.error({
81
+ status: Status.internal_server_error,
82
+ headers: { "Content-Type": "application/json" },
83
+ message: "Internal Server Error",
84
+ type: "InternalServerError",
85
+ });
86
+ return this;
87
+ }
88
+ }
89
+
51
90
  /* @docs:
52
91
  * @nav: Backend
53
92
  @parent: Utils
@@ -119,7 +158,7 @@ export class APIError extends Error {
119
158
 
120
159
  // Define interface with overloads
121
160
  interface UtilsInt {
122
- "APIError": typeof APIError; // Replace with actual type
161
+ "APIError": typeof ExternalError; // Replace with actual type
123
162
 
124
163
  // Overload signatures in interface
125
164
  clean_endpoint(endpoint: undefined): undefined;
@@ -136,7 +175,7 @@ interface UtilsInt {
136
175
  export const Utils: UtilsInt = {
137
176
 
138
177
  // An error that may be shown to the frontend user.
139
- "APIError": APIError,
178
+ "APIError": ExternalError,
140
179
 
141
180
  // Clean an endpoint url.
142
181
  clean_endpoint(endpoint) {
@@ -7,7 +7,7 @@
7
7
  // Exports.
8
8
 
9
9
  // Create volt lib.
10
- export { APIError } from "./utils.js"
10
+ export { ExternalError as APIError } from "./utils.js"
11
11
  export * from "./status.js"
12
12
  export * from "./meta.js"
13
13
  export * from "./splash_screen.js"
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "author": "Daan van den Bergh",
3
3
  "name": "@vandenberghinc/volt",
4
- "version": "1.1.13",
4
+ "version": "1.1.15",
5
5
  "description": "",
6
6
  "type": "module",
7
7
  "types": "./backend/dist/esm/volt.d.ts",