Home / File/ error-handling.js — svelte Source File

error-handling.js — svelte Source File

Architecture documentation for error-handling.js, a javascript file in the svelte codebase. 6 imports, 4 dependents.

File javascript ClientRuntime Hydration 6 imports 4 dependents 4 functions

Entity Profile

Dependency Diagram

graph LR
  a502a1d2_db34_608a_1001_5401b198ec66["error-handling.js"]
  73865c3c_2786_c9ac_d34f_b51d28b3a29e["constants.js"]
  a502a1d2_db34_608a_1001_5401b198ec66 --> 73865c3c_2786_c9ac_d34f_b51d28b3a29e
  9a9bbc27_46b6_021c_6d77_f736ed4b40f0["operations.js"]
  a502a1d2_db34_608a_1001_5401b198ec66 --> 9a9bbc27_46b6_021c_6d77_f736ed4b40f0
  984fb981_b6f6_b4f9_e92f_deca1946ed7f["constants.js"]
  a502a1d2_db34_608a_1001_5401b198ec66 --> 984fb981_b6f6_b4f9_e92f_deca1946ed7f
  cb946435_ce66_d1e8_6bee_287bdb07e7c5["utils.js"]
  a502a1d2_db34_608a_1001_5401b198ec66 --> cb946435_ce66_d1e8_6bee_287bdb07e7c5
  bde4209f_8ffc_1594_4024_b1835a44bcf6["runtime.js"]
  a502a1d2_db34_608a_1001_5401b198ec66 --> bde4209f_8ffc_1594_4024_b1835a44bcf6
  c9866d91_a204_fa55_a9e3_6bcc6aaaec1e["esm-env"]
  a502a1d2_db34_608a_1001_5401b198ec66 --> c9866d91_a204_fa55_a9e3_6bcc6aaaec1e
  6d3d606a_fb7a_54af_1ece_f1eb12f174d1["boundary.js"]
  6d3d606a_fb7a_54af_1ece_f1eb12f174d1 --> a502a1d2_db34_608a_1001_5401b198ec66
  1ad3e508_c069_abae_2e4a_bd17c8892e18["async.js"]
  1ad3e508_c069_abae_2e4a_bd17c8892e18 --> a502a1d2_db34_608a_1001_5401b198ec66
  d8e42d9d_2e3c_635c_19d3_b946a4341c0f["batch.js"]
  d8e42d9d_2e3c_635c_19d3_b946a4341c0f --> a502a1d2_db34_608a_1001_5401b198ec66
  bde4209f_8ffc_1594_4024_b1835a44bcf6["runtime.js"]
  bde4209f_8ffc_1594_4024_b1835a44bcf6 --> a502a1d2_db34_608a_1001_5401b198ec66
  style a502a1d2_db34_608a_1001_5401b198ec66 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/** @import { Derived, Effect } from '#client' */
/** @import { Boundary } from './dom/blocks/boundary.js' */
import { DEV } from 'esm-env';
import { FILENAME } from '../../constants.js';
import { is_firefox } from './dom/operations.js';
import { ERROR_VALUE, BOUNDARY_EFFECT, EFFECT_RAN } from './constants.js';
import { define_property, get_descriptor } from '../shared/utils.js';
import { active_effect, active_reaction } from './runtime.js';

const adjustments = new WeakMap();

/**
 * @param {unknown} error
 */
export function handle_error(error) {
	var effect = active_effect;

	// for unowned deriveds, don't throw until we read the value
	if (effect === null) {
		/** @type {Derived} */ (active_reaction).f |= ERROR_VALUE;
		return error;
	}

	if (DEV && error instanceof Error && !adjustments.has(error)) {
		adjustments.set(error, get_adjustments(error, effect));
	}

	if ((effect.f & EFFECT_RAN) === 0) {
		// if the error occurred while creating this subtree, we let it
		// bubble up until it hits a boundary that can handle it
		if ((effect.f & BOUNDARY_EFFECT) === 0) {
			if (DEV && !effect.parent && error instanceof Error) {
				apply_adjustments(error);
			}

			throw error;
		}

		/** @type {Boundary} */ (effect.b).error(error);
	} else {
		// otherwise we bubble up the effect tree ourselves
		invoke_error_boundary(error, effect);
	}
}

/**
 * @param {unknown} error
 * @param {Effect | null} effect
 */
export function invoke_error_boundary(error, effect) {
	while (effect !== null) {
		if ((effect.f & BOUNDARY_EFFECT) !== 0) {
			try {
				/** @type {Boundary} */ (effect.b).error(error);
				return;
			} catch (e) {
				error = e;
			}
		}

		effect = effect.parent;
	}

	if (DEV && error instanceof Error) {
		apply_adjustments(error);
	}

	throw error;
}

/**
 * Add useful information to the error message/stack in development
 * @param {Error} error
 * @param {Effect} effect
 */
function get_adjustments(error, effect) {
	const message_descriptor = get_descriptor(error, 'message');

	// if the message was already changed and it's not configurable we can't change it
	// or it will throw a different error swallowing the original error
	if (message_descriptor && !message_descriptor.configurable) return;

	var indent = is_firefox ? '  ' : '\t';
	var component_stack = `\n${indent}in ${effect.fn?.name || '<unknown>'}`;
	var context = effect.ctx;

	while (context !== null) {
		component_stack += `\n${indent}in ${context.function?.[FILENAME].split('/').pop()}`;
		context = context.p;
	}

	return {
		message: error.message + `\n${component_stack}\n`,
		stack: error.stack
			?.split('\n')
			.filter((line) => !line.includes('svelte/src/internal'))
			.join('\n')
	};
}

/**
 * @param {Error} error
 */
function apply_adjustments(error) {
	const adjusted = adjustments.get(error);

	if (adjusted) {
		define_property(error, 'message', {
			value: adjusted.message
		});

		define_property(error, 'stack', {
			value: adjusted.stack
		});
	}
}

Domain

Subdomains

Frequently Asked Questions

What does error-handling.js do?
error-handling.js is a source file in the svelte codebase, written in javascript. It belongs to the ClientRuntime domain, Hydration subdomain.
What functions are defined in error-handling.js?
error-handling.js defines 4 function(s): apply_adjustments, get_adjustments, handle_error, invoke_error_boundary.
What does error-handling.js depend on?
error-handling.js imports 6 module(s): constants.js, constants.js, esm-env, operations.js, runtime.js, utils.js.
What files import error-handling.js?
error-handling.js is imported by 4 file(s): async.js, batch.js, boundary.js, runtime.js.
Where is error-handling.js in the architecture?
error-handling.js is located at packages/svelte/src/internal/client/error-handling.js (domain: ClientRuntime, subdomain: Hydration, directory: packages/svelte/src/internal/client).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free