errors.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import {
  2. type CompilerError,
  3. DOMErrorCodes,
  4. type SourceLocation,
  5. createCompilerError,
  6. } from '@vue/compiler-dom'
  7. export interface SSRCompilerError extends CompilerError {
  8. code: SSRErrorCodes
  9. }
  10. export function createSSRCompilerError(
  11. code: SSRErrorCodes,
  12. loc?: SourceLocation,
  13. ) {
  14. return createCompilerError(code, loc, SSRErrorMessages) as SSRCompilerError
  15. }
  16. export enum SSRErrorCodes {
  17. X_SSR_UNSAFE_ATTR_NAME = 65 /* DOMErrorCodes.__EXTEND_POINT__ */,
  18. X_SSR_NO_TELEPORT_TARGET,
  19. X_SSR_INVALID_AST_NODE,
  20. }
  21. if (__TEST__) {
  22. // esbuild cannot infer enum increments if first value is from another
  23. // file, so we have to manually keep them in sync. this check ensures it
  24. // errors out if there are collisions.
  25. if (SSRErrorCodes.X_SSR_UNSAFE_ATTR_NAME < DOMErrorCodes.__EXTEND_POINT__) {
  26. throw new Error(
  27. `SSRErrorCodes need to be updated to ${
  28. DOMErrorCodes.__EXTEND_POINT__ + 1
  29. } to match extension point from core DOMErrorCodes.`,
  30. )
  31. }
  32. }
  33. export const SSRErrorMessages: { [code: number]: string } = {
  34. [SSRErrorCodes.X_SSR_UNSAFE_ATTR_NAME]: `Unsafe attribute name for SSR.`,
  35. [SSRErrorCodes.X_SSR_NO_TELEPORT_TARGET]: `Missing the 'to' prop on teleport element.`,
  36. [SSRErrorCodes.X_SSR_INVALID_AST_NODE]: `Invalid AST node during SSR transform.`,
  37. }