🆙 Update better error handeling

This commit is contained in:
duckietm
2026-03-19 09:40:49 +01:00
parent b47ac17a46
commit 5b139dfef8
9 changed files with 240 additions and 70 deletions
+43 -17
View File
@@ -77,19 +77,10 @@ export class AssetManager implements IAssetManager
{ {
if(!urls || !urls.length) return Promise.resolve(true); if(!urls || !urls.length) return Promise.resolve(true);
try
{
await Promise.all(urls.map(url => this.downloadAsset(url))); await Promise.all(urls.map(url => this.downloadAsset(url)));
return true; return true;
} }
catch (err)
{
NitroLogger.error(err);
}
return false;
}
public async downloadAsset(url: string): Promise<boolean> public async downloadAsset(url: string): Promise<boolean>
{ {
@@ -123,9 +114,18 @@ export class AssetManager implements IAssetManager
if(url.endsWith('.nitro') || url.endsWith('.gif')) if(url.endsWith('.nitro') || url.endsWith('.gif'))
{ {
const response = await fetch(url); let response: Response;
if(!response || response.status !== 200) return false; try
{
response = await fetch(url);
}
catch(fetchErr)
{
throw new Error(`Could not fetch "${ url }" — is the URL correct and the server reachable? (${ fetchErr.message })`);
}
if(!response || response.status !== 200) throw new Error(`Failed to load "${ url }" — server returned HTTP ${ response?.status ?? 'no response' }`);
const arrayBuffer = await response.arrayBuffer(); const arrayBuffer = await response.arrayBuffer();
@@ -136,20 +136,48 @@ export class AssetManager implements IAssetManager
await this.processAsset(nitroBundle.texture, nitroBundle.jsonFile as IAssetData); await this.processAsset(nitroBundle.texture, nitroBundle.jsonFile as IAssetData);
} }
else else
{
try
{ {
const animatedGif = AnimatedGIF.fromBuffer(arrayBuffer); const animatedGif = AnimatedGIF.fromBuffer(arrayBuffer);
const texture = animatedGif.texture; const texture = animatedGif.texture;
if(texture) this.setTexture(url, texture); if(texture) this.setTexture(url, texture);
} }
catch(gifErr)
{
const texture = await Assets.load<Texture>(url);
if(texture) this.setTexture(url, texture);
}
}
} }
else if(url.endsWith('.json')) else if(url.endsWith('.json'))
{ {
const response = await fetch(url); let response: Response;
if(!response || response.status !== 200) return false; try
{
response = await fetch(url);
}
catch(fetchErr)
{
throw new Error(`Could not fetch "${ url }" — is the URL correct and the server reachable? (${ fetchErr.message })`);
}
if(!response || response.status !== 200) throw new Error(`Failed to load "${ url }" — server returned HTTP ${ response?.status ?? 'no response' }`);
let data: IAssetData;
try
{
data = await response.json() as IAssetData;
}
catch(parseErr)
{
throw new Error(`Invalid JSON in "${ url }" — the URL may be wrong and returning an HTML page instead of JSON (${ parseErr.message })`);
}
const data = await response.json() as IAssetData;
let texture: Texture = null; let texture: Texture = null;
const imagePath = data?.spritesheet?.meta?.image; const imagePath = data?.spritesheet?.meta?.image;
const fallbackImagePath = ((data?.name && data.name.length > 0) const fallbackImagePath = ((data?.name && data.name.length > 0)
@@ -174,9 +202,7 @@ export class AssetManager implements IAssetManager
} }
catch (err) catch (err)
{ {
NitroLogger.error(err); throw new Error(`Asset loading failed for "${ url }": ${ err.message || err }`);
return false;
} }
} }
@@ -29,13 +29,31 @@ export class AvatarAssetDownloadManager
const url = GetConfiguration().getValue<string>('avatar.figuremap.url'); const url = GetConfiguration().getValue<string>('avatar.figuremap.url');
if(!url || !url.length) throw new Error('Invalid figure map url'); if(!url || !url.length) throw new Error('Missing "avatar.figuremap.url" in config — add the figure map URL to your renderer-config.json');
const response = await fetch(url); let response: Response;
if(response.status !== 200) throw new Error('Invalid figure map file'); try
{
response = await fetch(url);
}
catch(fetchErr)
{
throw new Error(`Could not fetch figure map from "${ url }" — check "avatar.figuremap.url" in renderer-config.json (${ fetchErr.message })`);
}
const responseData = await response.json(); if(response.status !== 200) throw new Error(`Failed to load figure map from "${ url }" — server returned HTTP ${ response.status }. Check "avatar.figuremap.url" in renderer-config.json`);
let responseData: any;
try
{
responseData = await response.json();
}
catch(parseErr)
{
throw new Error(`Invalid JSON in figure map "${ url }" — the URL may be wrong. Check "avatar.figuremap.url" in renderer-config.json (${ parseErr.message })`);
}
this.processFigureMap(responseData.libraries); this.processFigureMap(responseData.libraries);
+38 -6
View File
@@ -70,14 +70,30 @@ export class AvatarRenderManager implements IAvatarRenderManager
const url = GetConfiguration().getValue<string>('avatar.actions.url'); const url = GetConfiguration().getValue<string>('avatar.actions.url');
if(!url || !url.length) throw new Error('Invalid avatar action url'); if(!url || !url.length) throw new Error('Missing "avatar.actions.url" in config — add the URL to your renderer-config.json');
const response = await fetch(url); let response: Response;
if(response.status !== 200) throw new Error('Invalid avatar action file'); try
{
response = await fetch(url);
}
catch(fetchErr)
{
throw new Error(`Could not fetch avatar actions from "${ url }" — check "avatar.actions.url" in renderer-config.json (${ fetchErr.message })`);
}
if(response.status !== 200) throw new Error(`Failed to load avatar actions from "${ url }" — server returned HTTP ${ response.status }. Check "avatar.actions.url" in renderer-config.json`);
try
{
this._structure.updateActions(await response.json()); this._structure.updateActions(await response.json());
} }
catch(parseErr)
{
throw new Error(`Invalid JSON from "${ url }" — the URL may be wrong and returning an HTML page instead of JSON. Check "avatar.actions.url" in renderer-config.json (${ parseErr.message })`);
}
}
private async loadFigureData(): Promise<void> private async loadFigureData(): Promise<void>
{ {
@@ -87,13 +103,29 @@ export class AvatarRenderManager implements IAvatarRenderManager
const url = GetConfiguration().getValue<string>('avatar.figuredata.url'); const url = GetConfiguration().getValue<string>('avatar.figuredata.url');
if(!url || !url.length) throw new Error('Invalid figure data url'); if(!url || !url.length) throw new Error('Missing "avatar.figuredata.url" in config — add the URL to your renderer-config.json');
const response = await fetch(url); let response: Response;
if(response.status !== 200) throw new Error('Invalid figure data file'); try
{
response = await fetch(url);
}
catch(fetchErr)
{
throw new Error(`Could not fetch figure data from "${ url }" — check "avatar.figuredata.url" in renderer-config.json (${ fetchErr.message })`);
}
if(response.status !== 200) throw new Error(`Failed to load figure data from "${ url }" — server returned HTTP ${ response.status }. Check "avatar.figuredata.url" in renderer-config.json`);
try
{
this._structure.figureData.appendJSON(await response.json()); this._structure.figureData.appendJSON(await response.json());
}
catch(parseErr)
{
throw new Error(`Invalid JSON from "${ url }" — the URL may be wrong and returning an HTML page instead of JSON. Check "avatar.figuredata.url" in renderer-config.json (${ parseErr.message })`);
}
this._structure.init(); this._structure.init();
} }
@@ -29,13 +29,31 @@ export class EffectAssetDownloadManager
const url = GetConfiguration().getValue<string>('avatar.effectmap.url'); const url = GetConfiguration().getValue<string>('avatar.effectmap.url');
if(!url || !url.length) throw new Error('Invalid effect map url'); if(!url || !url.length) throw new Error('Missing "avatar.effectmap.url" in config — add the effect map URL to your renderer-config.json');
const response = await fetch(url); let response: Response;
if(response.status !== 200) throw new Error('Invalid effect map file'); try
{
response = await fetch(url);
}
catch(fetchErr)
{
throw new Error(`Could not fetch effect map from "${ url }" — check "avatar.effectmap.url" in renderer-config.json (${ fetchErr.message })`);
}
const responseData = await response.json(); if(response.status !== 200) throw new Error(`Failed to load effect map from "${ url }" — server returned HTTP ${ response.status }. Check "avatar.effectmap.url" in renderer-config.json`);
let responseData: any;
try
{
responseData = await response.json();
}
catch(parseErr)
{
throw new Error(`Invalid JSON in effect map "${ url }" — the URL may be wrong. Check "avatar.effectmap.url" in renderer-config.json (${ parseErr.message })`);
}
this.processEffectMap(responseData.effects); this.processEffectMap(responseData.effects);
@@ -22,27 +22,52 @@ export class ConfigurationManager implements IConfigurationManager
try try
{ {
this.resetConfiguration(); this.resetConfiguration();
this.parseConfiguration(this.getDefaultConfig(), true);
const defaultConfig = this.getDefaultConfig();
if(!defaultConfig) throw new Error('Missing NitroConfig: make sure window.NitroConfig is defined in index.html');
this.parseConfiguration(defaultConfig, true);
const configurationUrls = this.getValue<string[]>('config.urls').slice(); const configurationUrls = this.getValue<string[]>('config.urls').slice();
if(!configurationUrls || !configurationUrls.length) throw new Error('Invalid configuration urls'); if(!configurationUrls || !configurationUrls.length) throw new Error('No config.urls defined in NitroConfig — expected an array like ["/renderer-config.json", "/ui-config.json"]');
for(const url of configurationUrls) for(const url of configurationUrls)
{ {
if(!url || !url.length) return; if(!url || !url.length) return;
const response = await fetch(url); let response: Response;
if(response.status !== 200) throw new Error('Invalid configuration file'); try
{
response = await fetch(url);
}
catch(fetchError)
{
throw new Error(`Failed to fetch config "${ url }" — check that the file exists and the server is reachable (${ fetchError.message })`);
}
this.parseConfiguration(await response.json()); if(response.status !== 200) throw new Error(`Failed to load config "${ url }" — server returned HTTP ${ response.status }`);
let json: any;
try
{
json = await response.json();
}
catch(parseError)
{
throw new Error(`Invalid JSON in config "${ url }" — check for syntax errors like trailing commas or missing quotes (${ parseError.message })`);
}
this.parseConfiguration(json);
} }
} }
catch (err) catch (err)
{ {
throw new Error(err); throw new Error(err.message || String(err));
} }
} }
@@ -16,7 +16,7 @@ export class LocalizationManager implements ILocalizationManager
{ {
const urls = GetConfiguration().getValue<string[]>('external.texts.url').slice(); const urls = GetConfiguration().getValue<string[]>('external.texts.url').slice();
if(!urls || !urls.length) throw new Error('Invalid localization urls'); if(!urls || !urls.length) throw new Error('Missing "external.texts.url" in config — add the localization URL to your ui-config.json');
for(let url of urls) for(let url of urls)
{ {
@@ -24,11 +24,31 @@ export class LocalizationManager implements ILocalizationManager
url = GetConfiguration().interpolate(url); url = GetConfiguration().interpolate(url);
const response = await fetch(url); let response: Response;
if(response.status !== 200) throw new Error('Invalid localization file'); try
{
response = await fetch(url);
}
catch(fetchErr)
{
throw new Error(`Could not fetch localization file "${ url }" — check "external.texts.url" in ui-config.json (${ fetchErr.message })`);
}
this.parseLocalization(await response.json()); if(response.status !== 200) throw new Error(`Failed to load localization file "${ url }" — server returned HTTP ${ response.status }. Check "external.texts.url" in ui-config.json`);
let data: any;
try
{
data = await response.json();
}
catch(parseErr)
{
throw new Error(`Invalid JSON in localization file "${ url }" — the URL may be wrong. Check "external.texts.url" in ui-config.json (${ parseErr.message })`);
}
this.parseLocalization(data);
} }
GetCommunication().registerMessageEvent(new BadgePointLimitsEvent(this.onBadgePointLimitsEvent.bind(this))); GetCommunication().registerMessageEvent(new BadgePointLimitsEvent(this.onBadgePointLimitsEvent.bind(this)));
@@ -36,7 +56,7 @@ export class LocalizationManager implements ILocalizationManager
catch (err) catch (err)
{ {
throw new Error(err); throw new Error(err.message || String(err));
} }
} }
@@ -18,13 +18,31 @@ export class FurnitureDataLoader
{ {
const url = GetConfiguration().getValue<string>('furnidata.url'); const url = GetConfiguration().getValue<string>('furnidata.url');
if(!url || !url.length) throw new Error('invalid furni data url'); if(!url || !url.length) throw new Error('Missing "furnidata.url" in config — add the furniture data URL to your renderer-config.json');
const response = await fetch(url); let response: Response;
if(response.status !== 200) throw new Error('Invalid furni data file'); try
{
response = await fetch(url);
}
catch(fetchErr)
{
throw new Error(`Could not fetch furniture data from "${ url }" — check "furnidata.url" in renderer-config.json (${ fetchErr.message })`);
}
const responseData = await response.json(); if(response.status !== 200) throw new Error(`Failed to load furniture data from "${ url }" — server returned HTTP ${ response.status }. Check "furnidata.url" in renderer-config.json`);
let responseData: any;
try
{
responseData = await response.json();
}
catch(parseErr)
{
throw new Error(`Invalid JSON in furniture data "${ url }" — the URL may be wrong. Check "furnidata.url" in renderer-config.json (${ parseErr.message })`);
}
if(responseData.roomitemtypes) this.parseFloorItems(responseData.roomitemtypes); if(responseData.roomitemtypes) this.parseFloorItems(responseData.roomitemtypes);
@@ -15,13 +15,31 @@ export class ProductDataLoader
{ {
const url = GetConfiguration().getValue<string>('productdata.url'); const url = GetConfiguration().getValue<string>('productdata.url');
if(!url || !url.length) throw new Error('invalid product data url'); if(!url || !url.length) throw new Error('Missing "productdata.url" in config — add the product data URL to your renderer-config.json');
const response = await fetch(url); let response: Response;
if(response.status !== 200) throw new Error('Invalid product data file'); try
{
response = await fetch(url);
}
catch(fetchErr)
{
throw new Error(`Could not fetch product data from "${ url }" — check "productdata.url" in renderer-config.json (${ fetchErr.message })`);
}
const responseData = await response.json(); if(response.status !== 200) throw new Error(`Failed to load product data from "${ url }" — server returned HTTP ${ response.status }. Check "productdata.url" in renderer-config.json`);
let responseData: any;
try
{
responseData = await response.json();
}
catch(parseErr)
{
throw new Error(`Invalid JSON in product data "${ url }" — the URL may be wrong. Check "productdata.url" in renderer-config.json (${ parseErr.message })`);
}
this.parseProducts(responseData.productdata); this.parseProducts(responseData.productdata);
} }
+8 -13
View File
@@ -1,30 +1,25 @@
export class NitroVersion export class NitroVersion
{ {
public static RENDERER_VERSION: string = '2.0.0'; public static RENDERER_VERSION: string = '3.0.0';
public static UI_VERSION: string = ''; public static UI_VERSION: string = '3.0.4';
public static sayHello(): void public static sayHello(): void
{ {
if(navigator.userAgent.toLowerCase().indexOf('chrome') > -1) if(navigator.userAgent.toLowerCase().indexOf('chrome') > -1)
{ {
const args = [ const args = [
`\n %c %c %c Nitro ${NitroVersion.UI_VERSION} - Renderer ${NitroVersion.RENDERER_VERSION} %c %c %c https://discord.nitrodev.co %c %c \n\n`, `\n %c NITRO %c UI ${NitroVersion.UI_VERSION} %c Renderer ${NitroVersion.RENDERER_VERSION} %c \n`,
'background: #ffffff; padding:5px 0;', 'background: #1a3a5c; color: #ffffff; font-size: 14px; font-weight: bold; padding: 8px 12px; border-radius: 6px 0 0 6px;',
'background: #ffffff; padding:5px 0;', 'background: #2a5f8f; color: #e0ecf8; font-size: 12px; padding: 8px 10px;',
'color: #ffffff; background: #000000; padding:5px 0;', 'background: #3d7ab5; color: #e0ecf8; font-size: 12px; padding: 8px 10px; border-radius: 0 6px 6px 0;',
'background: #ffffff; padding:5px 0;', 'background: transparent;'
'background: #ffffff; padding:5px 0;',
'background: #000000; padding:5px 0;',
'background: #ffffff; padding:5px 0;',
'background: #ffffff; padding:5px 0;'
]; ];
self.console.log(...args); self.console.log(...args);
} }
else if(self.console) else if(self.console)
{ {
self.console.log(`Nitro ${NitroVersion.UI_VERSION} - Renderer ${NitroVersion.RENDERER_VERSION} `); self.console.log(`Nitro UI ${NitroVersion.UI_VERSION} - Renderer ${NitroVersion.RENDERER_VERSION}`);
} }
} }
} }