Improve spellcheck error handling and types (#21012)

GitOrigin-RevId: 0227122154f139401793aae2c8ca9c9960fe5e98
This commit is contained in:
Alf Eaton
2024-10-14 11:09:46 +00:00
committed by Copybot
parent 0131178491
commit ecb94d6525
5 changed files with 136 additions and 52 deletions
@@ -19,6 +19,7 @@ import { SpellChecker } from './spellchecker'
import { parserWatcher } from '../wait-for-parser'
import type { HunspellManager } from '@/features/source-editor/hunspell/HunspellManager'
import { debugConsole } from '@/utils/debugging'
import { captureException } from '@/infrastructure/error-reporter'
type Options = {
spellCheckLanguage?: string
@@ -81,9 +82,15 @@ const spellCheckerField = StateField.define<SpellChecker | null>({
)
: null
} else if (effect.is(addIgnoredWord)) {
value?.addWord(effect.value.text).catch(debugConsole.error)
value?.addWord(effect.value.text).catch(error => {
captureException(error)
debugConsole.error(error)
})
} else if (effect.is(removeIgnoredWord)) {
value?.removeWord(effect.value.text).catch(debugConsole.error)
value?.removeWord(effect.value.text).catch(error => {
captureException(error)
debugConsole.error(error)
})
}
}
return value
@@ -14,6 +14,7 @@ import {
import { waitForParser } from '../wait-for-parser'
import { debugConsole } from '@/utils/debugging'
import type { HunspellManager } from '../../hunspell/HunspellManager'
import { captureException } from '@/infrastructure/error-reporter'
/*
* Spellchecker, handles updates, schedules spelling checks
@@ -120,10 +121,14 @@ export class SpellChecker {
type: 'spell',
words: unknownWords.map(word => word.text),
},
(result: any) => {
result => {
if (!signal.aborted) {
if (result.error) {
if ('error' in result) {
debugConsole.error(result.error)
captureException(
new Error('Error running spellcheck for word'),
{ language: this.language }
)
} else {
processResult(result.misspellings)
}
@@ -148,10 +153,10 @@ export class SpellChecker {
return new Promise<{ suggestions: string[] }>((resolve, reject) => {
if (this.hunspellManager) {
this.hunspellManager.send({ type: 'suggest', word }, result => {
if ((result as { error: true }).error) {
reject(new Error())
if ('error' in result) {
reject(new Error('Error finding spelling suggestions for word'))
} else {
resolve(result as { suggestions: string[] })
resolve(result)
}
})
}
@@ -162,8 +167,8 @@ export class SpellChecker {
return new Promise<void>((resolve, reject) => {
if (this.hunspellManager) {
this.hunspellManager.send({ type: 'add_word', word }, result => {
if ((result as { error: true }).error) {
reject(new Error())
if ('error' in result) {
reject(new Error('Error adding word to spellcheck'))
} else {
resolve()
}
@@ -176,8 +181,8 @@ export class SpellChecker {
return new Promise<void>((resolve, reject) => {
if (this.hunspellManager) {
this.hunspellManager.send({ type: 'remove_word', word }, result => {
if ((result as { error: true }).error) {
reject(new Error())
if ('error' in result) {
reject(new Error('Error removing word from spellcheck'))
} else {
resolve()
}
@@ -13,6 +13,8 @@ import classnames from 'classnames'
import { useFeatureFlag } from '@/shared/context/split-test-context'
import { sendMB } from '@/infrastructure/event-tracking'
import SpellingSuggestionsFeedback from './spelling-suggestions-feedback'
import { captureException } from '@/infrastructure/error-reporter'
import { debugConsole } from '@/utils/debugging'
const ITEMS_TO_SHOW = 8
@@ -51,15 +53,23 @@ export const SpellingSuggestions: FC<{
useEffect(() => {
if (!word.suggestions) {
spellChecker?.suggest(word.text).then(result => {
setSuggestions(result.suggestions.slice(0, ITEMS_TO_SHOW))
setWaiting(false)
sendMB('spelling-suggestion-shown', {
language: spellCheckLanguage,
count: result.suggestions.length,
// word: transaction.state.sliceDoc(mark.from, mark.to),
spellChecker
?.suggest(word.text)
.then(result => {
setSuggestions(result.suggestions.slice(0, ITEMS_TO_SHOW))
setWaiting(false)
sendMB('spelling-suggestion-shown', {
language: spellCheckLanguage,
count: result.suggestions.length,
// word: transaction.state.sliceDoc(mark.from, mark.to),
})
})
.catch(error => {
captureException(error, {
language: spellCheckLanguage,
})
debugConsole.error(error)
})
})
}
}, [word, spellChecker, spellCheckLanguage])