Invite-Only Early Access — Think Throo GitHub App is currently invite-only. Request access here.
2025May

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. Email: ramu.narasinga@gmail.com

Tired of AI-generated code that works but nobody understands?

I spent 3+ years studying OSS codebases and wrote 350+ articles on what makes them production-grade. I built an open source tool that reviews your PR against your existing codebase patterns.

Your codebase. Your patterns. Enforced.

Get started for free —thinkthroo.com

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