{"record":{"id":"d0a8ee9af6c3511d","repo":"marktext/marktext","slug":"expected-non-empty-language-for-spell-checker","errorCode":null,"errorMessage":"Expected non-empty language for spell checker.","messagePattern":"Expected non-empty language for spell checker\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"packages/desktop/src/renderer/src/spellchecker/index.ts","lineNumber":79,"sourceCode":"    }\n    return ''\n  }\n\n  set lang(lang: string) {\n    this.currentSpellcheckerLanguage = lang\n  }\n\n  /**\n   * Explicitly switch the language to a specific language.\n   *\n   * NOTE: This function can throw an exception.\n   */\n  async switchLanguage(lang: string): Promise<boolean> {\n    if (isOsx) {\n      // NB: macOS uses the OS spell checker and detects language automatically.\n      return true\n    } else if (!lang) {\n      throw new Error('Expected non-empty language for spell checker.')\n    } else if (this.isEnabled) {\n      await window.electron.ipcRenderer.invoke('mt::spellchecker-switch-language', lang)\n      this.lang = lang\n      return true\n    }\n    return false\n  }\n\n  /**\n   * Returns a list of available dictionaries.\n   */\n  static async getAvailableDictionaries(): Promise<string[]> {\n    if (isOsx) {\n      // NB: macOS uses the OS spell checker and detects language automatically.\n      return []\n    }\n    return window.electron.ipcRenderer.invoke('mt::spellchecker-get-available-dictionaries')\n  }","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/marktext/marktext/blob/e52106fd1cdcbd33c1258b7b0cdc7013c4c5d86c/packages/desktop/src/renderer/src/spellchecker/index.ts#L61-L97","documentation":"Thrown by SpellChecker.switchLanguage() on non-macOS platforms when the supplied `lang` is falsy. macOS short-circuits (OS spell checker auto-detects). On Windows/Linux an empty language cannot be forwarded to the Chromium spell checker IPC, so it is rejected before invoking mt::spellchecker-switch-language.","triggerScenarios":"Calling switchLanguage('') or switchLanguage(undefined); currentSpellcheckerLanguage is empty and activateSpellchecker() is called with no argument (it passes this.currentSpellcheckerLanguage which defaults to '').","commonSituations":"First-run on Windows/Linux before a language has been chosen. A preferences value of language: '' propagated to the spell checker. A bus event delivers an empty languageCode.","solutions":["Pass a concrete language code (e.g. 'en-US') to switchLanguage / activateSpellchecker.","In activateSpellchecker, default to a detected system language when the stored value is empty.","Validate languageCode in switchSpellcheckLanguage (editor.vue) before invoking the spellchecker.","Ensure preferences.language is non-empty before initializing the SpellChecker."],"exampleFix":"// before\nasync switchLanguage(lang: string): Promise<boolean> {\n  if (isOsx) return true\n  else if (!lang) throw new Error('Expected non-empty language for spell checker.')\n}\n// after — default + validate\nasync switchLanguage(lang: string): Promise<boolean> {\n  if (isOsx) return true\n  const code = (lang || this.currentSpellcheckerLanguage || 'en-US').trim()\n  if (!code) throw new Error('Expected non-empty language for spell checker.')\n}","handlingStrategy":"validation","validationCode":"function nonEmptyLang(lang: string | undefined | null): string | null {\n  const v = (lang || '').trim()\n  return v.length ? v : null\n}","typeGuard":"function isNonEmptyLang(lang: unknown): lang is string {\n  return typeof lang === 'string' && lang.trim().length > 0\n}","tryCatchPattern":null,"preventionTips":["Default currentSpellcheckerLanguage to a system-detected value, not ''.","Validate languageCode in switchSpellcheckLanguage before calling the spellchecker.","Hide the language switcher on macOS (OS auto-detects) and when disabled.","Ensure preferences.language is non-empty before initializing SpellChecker."],"tags":["spellchecker","i18n","validation","renderer"],"backgroundTag":null,"analyzedSha":"e52106fd1cdcbd33c1258b7b0cdc7013c4c5d86c","analyzedAt":"2026-08-12T13:58:32.443Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}