Skip to content

Commit

Permalink
chore: fallback to default settings if error
Browse files Browse the repository at this point in the history
chore: log_error shows more info in UI
  • Loading branch information
danielo515 committed Oct 24, 2023
1 parent c8f1923 commit 68fa95b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/core/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ const ModalFormSettingsSchema = object({

type ModalFormSettingsPartial = Output<typeof ModalFormSettingsSchema>;

const DEFAULT_SETTINGS: ModalFormSettings = {
export const DEFAULT_SETTINGS: ModalFormSettings = {
editorPosition: 'right',
formDefinitions: [],
};

export function parseSettings(maybeSettings: unknown): E.Either<ValiError, ModalFormSettingsPartial> {
if (maybeSettings === null) return E.right(DEFAULT_SETTINGS)
if (maybeSettings === null) return E.right({ ...DEFAULT_SETTINGS })
;
return E.tryCatch(() => parse(ModalFormSettingsSchema, { ...DEFAULT_SETTINGS, ...maybeSettings }), e => e as ValiError);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { EDIT_FORM_VIEW, EditFormView } from "src/views/EditFormView";
import { MANAGE_FORMS_VIEW, ManageFormsView } from "src/views/ManageFormsView";
import { ModalFormError } from "src/utils/Error";
import { formNeedsMigration, type FormDefinition, migrateToLatest } from "src/core/formDefinition";
import { parseSettings, type ModalFormSettings, type OpenPosition } from "src/core/settings";
import { parseSettings, type ModalFormSettings, type OpenPosition, DEFAULT_SETTINGS } from "src/core/settings";
import { log_error, log_notice } from "./utils/Log";
import * as E from "fp-ts/Either";
import { pipe } from "fp-ts/function";
Expand Down Expand Up @@ -125,9 +125,9 @@ export default class ModalFormPlugin extends Plugin {
const data = await this.loadData();
const settingsParsed = parseSettings(data);
if (E.isLeft(settingsParsed)) {
const error = new ModalFormError('Settings are not valid, check the errors', JSON.stringify(settingsParsed.left.issues))
const error = new ModalFormError('Settings are not valid, check the errors', JSON.stringify(settingsParsed.left.issues, null, 2))
log_error(error)
throw error;
return { ...DEFAULT_SETTINGS };
}
const settings = settingsParsed.right;
const migrationIsNeeded = settings.formDefinitions.some(formNeedsMigration);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function log_update(msg: string): void {

export function log_error(e: Error | ModalFormError): void {
if (e instanceof ModalFormError && e.console_msg) {
log_notice('Modal from error', e.message)
log_notice('Modal from error: ', e.message + "\n" + e.console_msg)
console.error(`Modal form Error:`, e.message, "\n", e.console_msg);
} else {
log_notice('Modal from error', e.message)
Expand Down

0 comments on commit 68fa95b

Please sign in to comment.