Blog
throwError utility in Inferno.js source code.

throwError utility in Inferno.js source code.

In this article, we review a commonly used utility named, throwError in Infero.js source code.

export function renderInternal(
  input: VNode | InfernoNode,
  parentDOM: ParentDOM,
  callback: (() => void) | null,
  context: ContextObject,
): void {
  // Development warning
  if (process.env.NODE_ENV !== 'production') {
    if (documentBody === parentDOM) {
      throwError(
        'you cannot render() to the "document.body". Use an empty element as a container instead.',
      );
    }
    if (isInvalid(parentDOM)) {
      throwError(
        `render target ( DOM ) is mandatory, received ${
          parentDOM === null ? 'null' : typeof parentDOM
        }`,
      );
    }
  }

This above code snippet is picked from inferno/src/DOM/rendering.ts

throwError

throwError accepts one parameter which is a string, a message explaining the type of error thrown.

throwError(
  `render target ( DOM ) is mandatory, received ${
    parentDOM === null ? 'null' : typeof parentDOM
  }`,
)

This throwError utility is used in mutiple places across the inferno codebase.

Declaration

This function is declared in a package, infero-shared/src/index.ts.

export function throwError(message?: string): void {
  if (!message) {
    message = ERROR_MSG;
  }
  throw new Error(`Inferno Error: ${message}`);
}

The message argument here is just a string, but you see that variable ERROR_MSG that is used a fallback in case there is no message, i.e., message is empty string or null or undefined? this ERROR_MSG is defined at the top of the same file.

export const ERROR_MSG =
  'a runtime error occured! Use Inferno in development environment to find the error.';

About me:

Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.

Configure features such as Changesets in your Next.js project using Think Throo CLI.

Email — ramu@thinkthroo.com

My Github —  https://github.com/ramu-narasinga

My website —  https://ramunarasinga.com

My YouTube channel —  https://www.youtube.com/@ramu-narasinga

Learning platform —  https://thinkthroo.com

Codebase Architecture —  https://app.thinkthroo.com/architecture

Best practices —  https://app.thinkthroo.com/best-practices

Production-grade projects —  https://app.thinkthroo.com/production-grade-projects

References

  1. https://github.com/infernojs/inferno/blob/929300deb5b911f940fe19bc285afb7863521bf7/packages/inferno/src/DOM/rendering.ts#L63

  2. https://github.com/infernojs/inferno/blob/master/packages/inferno-shared/src/index.ts#L40