Skip to content

Commit

Permalink
error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunkLightTuna committed Dec 16, 2023
1 parent 135cfb5 commit c60ba06
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 45 deletions.
15 changes: 13 additions & 2 deletions languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,25 @@
"oronder.Fetching-Discord-Ids": "Fetching Discord Ids",
"oronder.Oronder-Configuration": "Oronder Configuration",
"oronder.Configure-Oronder": "Configure Oronder",
"oronder.Configure-Hint": "This will fetch Discord Ids for Users whose Foundry User names match their Discord name. Support for Pomelo Nicknames pending.",
"oronder.Discord-Ids": "Discord Ids",
"oronder.Oronder-Bot-Config": "Oronder Bot Config",
"oronder.Invalid-Auth": "Invalid Server Id and/or Auth Token. Have you run \"/admin init\" in Discord?",
"oronder.Server-Id-NaN": "Discord Server Id must be a number.",
"oronder.Unexpected-Error": "Unexpected Error Occurred!",
"oronder.Invalid-Discord-Ids": "Invalid Discord Ids for players",
"oronder.Could-Not-Be-Found": "could not be found in Discord server.",
"oronder.Discord-Server-Link-Title": "Oronder Discord",
"oronder.Discord-Bot-Link-Title": "Oronder Bot",
"oronder.Auth-Token-Empty": "Auth Token cannot be empty.",
"oronder.No-Players-To-Sync": "All Players have Discord Ids. Nothing to update."
"oronder.No-Players-To-Sync": "All Players have Discord Ids. Nothing to update.",
"oronder.Skipping-Sync-For": "Skipping sync for",
"oronder.Failed-To-Sync": "failed to sync!",
"oronder.Synced": "Synced",
"oronder.NPC": "Actor is NPC.",
"oronder.No-Owner": "Owner's Discord ID not found.",
"oronder.No-Change": "Actor has no Significant Change.",
"oronder.No-Level": "Actor has no Level.",
"oronder.No-Race": "Actor has no Race.",
"oronder.No-Background": "Actor has no Background.",
"oronder.No-Class": "Actor has no Class."
}
1 change: 0 additions & 1 deletion src/constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ export const ID_MAP = "id_map"

export const ACTORS = "actors"
export const ORONDER_BASE_URL = window.location.host === 'localhost:65434' ? 'http://localhost:65435' : 'https://api.oronder.com'
// export const ORONDER_BASE_URL = 'https://api.oronder.com'
14 changes: 5 additions & 9 deletions src/settings-form-application.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class OronderSettingsFormApplication extends FormApplication {
}

static get defaultOptions() {
return mergeObject(super.defaultOptions, {
return foundry.utils.mergeObject(super.defaultOptions, {
id: "oronder-options",
template: `modules/${MODULE_ID}/templates/settings-form-application.hbs`,
width: 580,
Expand Down Expand Up @@ -95,17 +95,15 @@ export class OronderSettingsFormApplication extends FormApplication {
const invalid_player_names = invalid_discord_ids.map(invalid_discord_id => {
return this.object.players.find(p => p.discord_id === invalid_discord_id).foundry_name
})
if (invalid_player_names.length) {
if (invalid_player_names.length===1) {
Logger.logError(
`${game.i18n.localize("oronder.Invalid-Discord-Ids")}: ${invalid_player_names.join(', ')}`
`${invalid_player_names[0]} ${game.i18n.localize("oronder.Could-Not-Be-Found")}`
)
} else {
valid_config = true
}
})
.catch(error => {
Logger.logError(error)
})
.catch(Logger.logError)

game.settings.set(MODULE_ID, VALID_CONFIG, valid_config)
game.settings.set(MODULE_ID, GUILD_ID, this.object.guild_id)
Expand Down Expand Up @@ -174,9 +172,7 @@ export class OronderSettingsFormApplication extends FormApplication {
this.object.players.find(p => p.foundry_name === foundry_name).discord_id = discord_user_id
}
})
.catch(e => {
Logger.logError(e)
})
.catch(Logger.logError)

this.object.fetch_button_icon = "fa-solid fa-rotate"
this.object.fetch_sync_disabled = false
Expand Down
2 changes: 1 addition & 1 deletion src/settings.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const registerSettings = async () => {
name: `${MODULE_ID}.Oronder-Configuration`,
label: `${MODULE_ID}.Configure-Oronder`,
icon: "fa-brands fa-discord",
hint: 'This will fetch Discord Ids for Users whose Foundry User names match their Discord name. Support for Pomelo Nicknames pending.',
hint: `${MODULE_ID}.Configure-Hint`,
scope: "client",
config: true,
type: OronderSettingsFormApplication,
Expand Down
60 changes: 42 additions & 18 deletions src/sync.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -190,30 +190,54 @@ export async function full_sync() {
}

export async function sync_actor(actor) {
if (actor.type !== "character" || !actor_to_discord_ids(actor).length)
const a = Date.now()
if (actor.type !== "character") {
Logger.log(
`${game.i18n.localize("oronder.Skipping-Sync-For")} ${actor.name}. ${game.i18n.localize("oronder.NPC")}`
);
return Promise.resolve()
}
if (!actor_to_discord_ids(actor).length) {
Logger.log(
`${game.i18n.localize("oronder.Skipping-Sync-For")} ${actor.name}. ${game.i18n.localize("oronder.No-Owner")}`
);
return Promise.resolve()
}

const old_hash = localStorage.getItem(`${ACTORS}.${actor.id}`)
const actor_obj = enrich_actor(actor)
const new_hash = hash(actor_obj)

if (
(!old_hash || old_hash !== new_hash) &&
actor_obj.details.level &&
actor_obj.details.race &&
actor_obj.details.background &&
Object.keys(actor_obj.classes).length
) {
return upload(actor_obj).then(response => {
if (response.ok) {
localStorage.setItem(`${ACTORS}.${actor.id}`, new_hash)
Logger.log(`Synced ${actor_obj.name}`);
} else {
Logger.logError(`${actor_obj.name} failed to sync!`);
}
}).catch(Logger.logError)
} else {
Logger.log(`Skipping sync for ${actor_obj.name}.`);
if (old_hash && old_hash === new_hash) {
Logger.log(`${game.i18n.localize("oronder.Skipping-Sync-For")} ${actor_obj.name}. ${game.i18n.localize("oronder.No-Change")}`);
return Promise.resolve()
}
if (!actor_obj.details.level) {
Logger.log(`${game.i18n.localize("oronder.Skipping-Sync-For")} ${actor_obj.name}. ${game.i18n.localize("oronder.No-Level")}`);
return Promise.resolve()
}
if (!actor_obj.details.race) {
Logger.log(`${game.i18n.localize("oronder.Skipping-Sync-For")} ${actor_obj.name}. ${game.i18n.localize("oronder.No-Race")}`);
return Promise.resolve()
}
if (!actor_obj.details.background) {
Logger.log(`${game.i18n.localize("oronder.Skipping-Sync-For")} ${actor_obj.name}. ${game.i18n.localize("oronder.No-Background")}`);
return Promise.resolve()
}
if (!Object.keys(actor_obj.classes).length) {
Logger.log(`${game.i18n.localize("oronder.Skipping-Sync-For")} ${actor_obj.name}. ${game.i18n.localize("oronder.No-Class")}`);
return Promise.resolve()
}

const p = upload(actor_obj).then(response => {
if (response.ok) {
localStorage.setItem(`${ACTORS}.${actor.id}`, new_hash)
Logger.log(`${game.i18n.localize("oronder.Synced")} ${actor_obj.name}`);
} else {
Logger.logError(`${actor_obj.name} ${game.i18n.localize("oronder.Failed-To-Sync")}`);
}
}).catch(Logger.logError)

Logger.log(`${actor.name}: ${Math.floor((Date.now() - a) / 1000)} seconds`)
return p
}
15 changes: 1 addition & 14 deletions src/util.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,16 @@ import objectHash from 'object-hash';
* Utility class to handle logging to console with an attached debug tag to identify module logs.
*/
export class Logger {
/**
* Sends an info log to the console.
* @param {String} logString The string to log as an info.
*/

static log(logString) {
console.log(..._processLog(logString));
}

/**
* Sends an error log to the console and displays an error UI notification.
* @param {String} logString The string to log as an error.
* @param options
*/
static logError(logString, options = {}) {
if (options.ui ?? true) ui.notifications.error(logString, {console: false});
if (options.console ?? true) console.error(..._processLog(logString));
}

/**
* Sends a warning log to the console and displays a warning UI notification.
* @param {String} logString The string to log as a warning.
* @param options
*/
static logWarning(logString, options = {}) {
if (options.ui ?? true) ui.notifications.warn(logString, {console: false});
if (options.console ?? true) console.warn(..._processLog(logString));
Expand Down

0 comments on commit c60ba06

Please sign in to comment.