Skip to content

Commit bd9702f

Browse files
committed
v1.12.9.0: Fix incorrect error messages before libWrapper.Ready
1 parent e2864f7 commit bd9702f

14 files changed

+310
-320
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# 1.12.9.0 (2022-09-11)
2+
3+
- Fix incorrect error messages when calling the libWrapper API before the `libWrapper.Ready` hook. ([Issue #72](https://github.com/ruipin/fvtt-lib-wrapper/issues/72))
4+
- Add unit test to catch this issue in the future.
5+
- Fix accidental 3 consecutive blank lines in error messages when a module has neither an `url` nor a `bugs` entry in its manifest.
6+
- Update NPM dependencies to latest version.
7+
18
# 1.12.8.0 (2022-09-04)
29

310
- Get rid of FVTT v10 deprecation warnings caused by legacy style package 'data' accesses.

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ To register a wrapper function, you should call the method `libWrapper.register(
312312
*
313313
* @param {string} package_id The package identifier, i.e. the 'id' field in your module/system/world's manifest.
314314
*
315-
* @param {number|string} target The target identifier, specifying which wrapper should be unregistered.
315+
* @param {number|string} target The target identifier, specifying which wrapper should be registered.
316316
*
317317
* This can be either:
318318
* 1. A unique target identifier obtained from a previous 'libWrapper.register' call.

module.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "lib-wrapper",
44
"title": "libWrapper",
55
"description": "Library for wrapping core Foundry VTT methods, meant to improve compatibility between packages that wrap the same methods.",
6-
"version": "1.12.8.0",
6+
"version": "1.12.9.0",
77
"author": "Rui Pinheiro",
88
"authors": [{
99
"name": "Rui Pinheiro",

package-lock.json

+211-282
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-5
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,15 @@
2929
"deep-equal": "^2.0.5",
3030
"lessc": "^1.0.2",
3131
"npm-force-resolutions": "0.0.10",
32-
"rollup": "^2.56.2",
32+
"rollup": "^2.79.0",
3333
"rollup-plugin-cleanup": "^3.2.1",
3434
"rollup-plugin-jscc": "^2.0.0",
3535
"rollup-plugin-terser": "^7.0.2",
3636
"tap-dot": "^2.0.0",
3737
"tap-spec": "^5.0.0",
38-
"tape": "^5.3.1",
39-
"tape-es": "^1.2.15"
38+
"tape": "^5.6.0",
39+
"tape-es": "^1.2.17"
4040
},
4141
"resolutions": {
42-
"trim": "^0.0.3",
43-
"terser": "^5.7.1"
4442
}
4543
}

src/errors/base_errors.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -139,19 +139,19 @@ export class LibWrapperPackageError extends LibWrapperError {
139139
let console_msg = `${console_ui_msg}\n\n${i18n.localize(`${key_prefix}.not-lw`)}\n\n`;
140140

141141
const info_url = package_info.url;
142-
if(typeof info_url === 'string') {
142+
const has_info = (typeof info_url === 'string');
143+
if(has_info) {
143144
console_msg += i18n.format(`${type_prefix}.info`, {type: pkg_type_i18n, url: info_url});
144145
}
145146

146147
const report_url = package_info.bugs;
147148
if(typeof report_url === 'string') {
148-
console_msg += '\n';
149149
console_msg += i18n.format(`${type_prefix}.report`, {url: report_url});
150150
}
151151
else {
152152
const community_support_msg = this.get_community_support_message();
153153
if(community_support_msg) {
154-
console_msg += '\n\n';
154+
if(has_info) console_msg += '\n\n';
155155
console_msg += i18n.localize(`${key_prefix}.community-support`);
156156
console_msg += '\n';
157157
console_msg += community_support_msg;

src/lib/api.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ function _get_package_info(package_id) {
286286
}
287287
// Sanity Check: Package must exist (single exception is lib-wrapper, since we register wrappers before 'init')
288288
else {
289-
if(!package_info.exists && game.modules?.size)
289+
if(!package_info.exists && globalThis.game?.modules?.size)
290290
throw new ERRORS.package(`Package '${package_id}' is not a valid package.`, package_info);
291291
}
292292

@@ -343,7 +343,7 @@ export class libWrapper {
343343
static get AlreadyOverriddenError() { return ERRORS.already_overridden; };
344344

345345
static get LibWrapperInvalidWrapperChainError() { return ERRORS.invalid_chain; };
346-
static get InvalidWrapperChainError () { return ERRORS.invalid_chain; };
346+
static get InvalidWrapperChainError() { return ERRORS.invalid_chain; };
347347

348348
/* Undocumented on purpose, do not use */
349349
static get onUnhandledError() { return onUnhandledError; };
@@ -385,7 +385,7 @@ export class libWrapper {
385385
*
386386
* @param {string} package_id The package identifier, i.e. the 'id' field in your module/system/world's manifest.
387387
*
388-
* @param {number|string} target The target identifier, specifying which wrapper should be unregistered.
388+
* @param {number|string} target The target identifier, specifying which wrapper should be registered.
389389
*
390390
* This can be either:
391391
* 1. A unique target identifier obtained from a previous 'libWrapper.register' call.
@@ -472,7 +472,7 @@ export class libWrapper {
472472

473473
// Validate we're allowed to register wrappers at this moment
474474
if(package_id != PACKAGE_ID && !libwrapper_ready)
475-
throw new ERRORS.package('Not allowed to register wrappers before the \'libWrapperReady\' hook fires', package_info);
475+
throw new ERRORS.package('Not allowed to register wrappers before the \'libWrapper.Ready\' hook fires', package_info);
476476

477477
// Validate other arguments
478478
if(typeof target !== 'string' && typeof target !== 'number')
@@ -619,6 +619,10 @@ export class libWrapper {
619619
// Get package information
620620
const package_info = _get_package_info(package_id);
621621

622+
// Validate we're allowed to unregister wrappers at this moment
623+
if(package_id != PACKAGE_ID && !libwrapper_ready)
624+
throw new ERRORS.package('Not allowed to unregister wrappers before the \'libWrapper.Ready\' hook fires', package_info);
625+
622626
// Validate arguments
623627
if(typeof target !== 'string' && typeof target !== 'number')
624628
throw new ERRORS.package('Parameter \'target\' must be a number or a string.', package_info);
@@ -649,6 +653,10 @@ export class libWrapper {
649653
// Get package information
650654
const package_info = _get_package_info(package_id);
651655

656+
// Validate we're allowed to unregister wrappers at this moment
657+
if(package_id != PACKAGE_ID && !libwrapper_ready)
658+
throw new ERRORS.package('Not allowed to unregister wrappers before the \'libWrapper.Ready\' hook fires', package_info);
659+
652660
// Clear package wrappers
653661
WRAPPERS.forEach((wrapper) => {
654662
this.unregister(package_info.id, wrapper.getter_id, false);
@@ -692,7 +700,7 @@ export class libWrapper {
692700

693701
// Validate we are allowed to call this method right now
694702
if(!libwrapper_ready)
695-
throw new ERRORS.package('Not allowed to ignore conflicts before the \'libWrapperReady\' hook fires', package_info);
703+
throw new ERRORS.package('Not allowed to ignore conflicts before the \'libWrapper.Ready\' hook fires', package_info);
696704

697705
// Convert parameters to arrays
698706
if(!Array.isArray(ignore_ids))
@@ -743,6 +751,7 @@ if(IS_UNITTEST) {
743751
libWrapper._UT_clear_ignores = (() => LibWrapperConflicts.clear_ignores());
744752
libWrapper._UT_TGT_SPLIT_REGEX = TGT_SPLIT_RE;
745753
libWrapper._UT_TGT_CLEANUP_REGEX = TGT_CLEANUP_RE;
754+
libWrapper._UT_setReady = ((rdy) => libwrapper_ready = rdy);
746755
}
747756
Object.freeze(libWrapper);
748757

src/shared

tests/test_ignore.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function setup() {
1414
libWrapper._UT_unwrap_all();
1515
libWrapper._UT_clear_ignores();
1616

17-
game.modules.clear();
17+
game.reset();
1818
globalThis.A = undefined;
1919
}
2020

tests/test_lib.js

+52-11
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,49 @@ import {load_priorities} from '../src/ui/settings.js';
1313

1414
function setup() {
1515
libWrapper._UT_unwrap_all();
16+
libWrapper._UT_setReady(true);
1617
load_priorities();
17-
18-
game.clear_modules();
18+
game.reset();
1919
}
2020

2121

22+
// Pre-ready functionality
23+
test_combinations('Library: Pre-ready', async function(t) {
24+
setup();
25+
26+
// Set libWrapper to not-ready, and mimic a non-initialised game
27+
libWrapper._UT_setReady(false);
28+
game.modules = undefined;
29+
game.ready = false;
30+
31+
// Check that we throw when libWrapper is not yet ready
32+
t.throws(
33+
() => libWrapper.register('some-module', 'A.prototype.x', () => {}, 'MIXED'),
34+
new RegExp("Not allowed to register wrappers before the 'libWrapper.Ready' hook fires"),
35+
"Calling 'register' before ready should throw"
36+
);
37+
38+
t.throws(
39+
() => libWrapper.unregister('some-module', 'A.prototype.x'),
40+
new RegExp("Not allowed to unregister wrappers before the 'libWrapper.Ready' hook fires"),
41+
"Calling 'unregister' before ready should throw"
42+
);
43+
44+
t.throws(
45+
() => libWrapper.unregister_all('some-module'),
46+
new RegExp("Not allowed to unregister wrappers before the 'libWrapper.Ready' hook fires"),
47+
"Calling 'unregister_all' before ready should throw"
48+
);
49+
50+
t.throws(
51+
() => libWrapper.ignore_conflicts('some-module', 'another-module', 'A.prototype.x'),
52+
new RegExp("Not allowed to ignore conflicts before the 'libWrapper.Ready' hook fires"),
53+
"Calling 'ignore_conflicts' before ready should throw"
54+
);
55+
56+
t.end();
57+
});
58+
2259

2360
// Main functionality of libWrapper
2461
test_combinations('Library: Main', async function (t) {
@@ -42,9 +79,11 @@ test_combinations('Library: Main', async function (t) {
4279
await chkr.call(a, 'x', ['m1:Mix:1','Orig',-2]);
4380

4481
// Registering the same method twice with the same module should fail
45-
t.throws(function() {
46-
libWrapper.register('m1', 'A.prototype.x', () => {});
47-
}, libWrapper.Error, 'Registering twice with same module should fail');
82+
t.throws(
83+
() => libWrapper.register('m1', 'A.prototype.x', () => {}),
84+
libWrapper.Error,
85+
'Registering twice with same module should fail'
86+
);
4887
await chkr.call(a, 'x', ['m1:Mix:1','Orig',-2]);
4988

5089
// Register WRAPPER
@@ -59,9 +98,11 @@ test_combinations('Library: Main', async function (t) {
5998

6099
// Registing another OVERRIDE should fail
61100
game.add_module('m4');
62-
t.throws(function() {
63-
libWrapper.register('m4', 'A.prototype.x', () => {}, 'OVERRIDE');
64-
}, libWrapper.AlreadyOverriddenError, 'Registering second override should fail');
101+
t.throws(
102+
() => libWrapper.register('m4', 'A.prototype.x', () => {}, 'OVERRIDE'),
103+
libWrapper.AlreadyOverriddenError,
104+
'Registering second override should fail'
105+
);
65106
await chkr.call(a, 'x', ['m2:Wrp:2','m1:Mix:1','m3:Ovr:3',-3]);
66107

67108
// Unless the module has a higher priority
@@ -126,11 +167,11 @@ test_combinations('Library: Main', async function (t) {
126167

127168

128169
// Test invalid getter
129-
t.throws(() => libWrapper.register('m1', 'A.prototype.xyz', ()=>{}), libWrapper.ModuleError, "Wrap invalid getter");
170+
t.throws(() => libWrapper.register('m1', 'A.prototype.xyz', ()=>{}), libWrapper.PackageError, "Wrap invalid getter");
130171

131172
// Test invalid setter
132-
t.throws(() => libWrapper.register('m1', 'A.prototype.x#set', ()=>{}), libWrapper.ModuleError, "Wrap invalid setter");
133-
t.throws(() => libWrapper.register('m1', 'A.prototype.xyz#set', ()=>{}), libWrapper.ModuleError, "Wrap invalid setter");
173+
t.throws(() => libWrapper.register('m1', 'A.prototype.x#set', ()=>{}), libWrapper.PackageError, "Wrap invalid setter");
174+
t.throws(() => libWrapper.register('m1', 'A.prototype.xyz#set', ()=>{}), libWrapper.PackageError, "Wrap invalid setter");
134175

135176

136177
// Done

tests/test_performance.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import '../src/lib/api.js';
1414

1515
function setup() {
1616
libWrapper._UT_unwrap_all();
17-
game.clear_modules();
17+
18+
game.reset();
1819
}
1920

2021

tests/test_shim.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import '../src/lib/api.js';
1919
function setup() {
2020
libWrapper._UT_unwrap_all();
2121

22-
game.clear_modules();
22+
game.reset();
2323
}
2424

2525

tests/test_wrapper.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import '../src/lib/api.js';
1111
function setup() {
1212
libWrapper._UT_unwrap_all();
1313

14-
game.modules.clear();
14+
game.reset();
1515
globalThis.A = undefined;
1616
}
1717

tests/utilities.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ class GameSettings {
6565

6666
class Game {
6767
constructor() {
68-
this.modules = new Map();
68+
this.reset();
69+
}
70+
71+
reset() {
6972
this.settings = new GameSettings();
7073
this.user = { isGM: true, userId: 12345 };
7174
this.userId = 12345;
@@ -85,6 +88,13 @@ class Game {
8588
}
8689
}
8790
this.ready = true;
91+
92+
this.clear_modules();
93+
}
94+
95+
clear_modules() {
96+
this.modules = new Map();
97+
this.add_module('lib-wrapper');
8898
}
8999

90100
add_module(nm) {
@@ -95,12 +105,7 @@ class Game {
95105
else
96106
mdl.data = { title: nm };
97107

98-
game.modules.set(nm, mdl);
99-
}
100-
101-
clear_modules() {
102-
this.modules.clear();
103-
this.add_module('lib-wrapper');
108+
this.modules.set(nm, mdl);
104109
}
105110
}
106111
globalThis.game = new Game();

0 commit comments

Comments
 (0)