import { repeat } from 'lodash' ; import { i18n } from '@osd/i18n' ; const endOfInputText = i18n . translate ( 'data.common.dql.errors.endOfInputText' , { defaultMessage : 'end of input' , } ) ; const grammarRuleTranslations = { fieldName : i18n . translate ( 'data.common.dql.errors.fieldNameText' , { defaultMessage : 'field name' , } ) , value : i18n . translate ( 'data.common.dql.errors.valueText' , { defaultMessage : 'value' , } ) , literal : i18n . translate ( 'data.common.dql.errors.literalText' , { defaultMessage : 'literal' , } ) , whitespace : i18n . translate ( 'data.common.dql.errors.whitespaceText' , { defaultMessage : 'whitespace' , } ) , } ; interface DQLSyntaxErrorData extends Error { found : string ; expected : DQLSyntaxErrorExpected [ ] | null ; location : any ; } interface DQLSyntaxErrorExpected { description : string ; } export class DQLSyntaxError extends Error { shortMessage : string ; constructor ( error , expression ) { let message = error . message ; if ( error . expected ) { const translatedExpectations = error . expected . map ( ( expected ) => { return grammarRuleTranslations [ expected . description ] || expected . description ; } ) ; const translatedExpectationText = translatedExpectations . join ( ', ' ) ; message = i18n . translate ( 'data.common.dql.errors.syntaxError' , { defaultMessage : 'Expected {expectedList} but {foundInput} found.' , values : { expectedList : translatedExpectationText , foundInput : error . found ? `"${ error . found }"` : endOfInputText , } , } ) ; } const fullMessage = [ message , expression , repeat ( '-' , error . location . start . offset ) + '^' ] . join ( '\n' ) ; super ( fullMessage ) ; this . name = 'DQLSyntaxError' ; this . shortMessage = message ; } }