Visualize Your Ideas With Code
feat: improve image error handling (#847)
Jacobdb09d5305a3c
diff --git a/packages/2d/src/components/Img.ts b/packages/2d/src/components/Img.tsindex fdb64e10..038ad98f 100644--- a/packages/2d/src/components/Img.ts+++ b/packages/2d/src/components/Img.ts@@ -14,7 +14,11 @@ import {SignalValue,SimpleSignal,} from '@motion-canvas/core/lib/signals';-import {useLogger, viaProxy} from '@motion-canvas/core/lib/utils';+import {+ useLogger,+ viaProxy,+ DetailedError,+} from '@motion-canvas/core/lib/utils';import imageWithoutSource from './__logs__/image-without-source.md';export interface ImgProps extends RectProps {@@ -173,7 +177,22 @@ export class Img extends Rect {DependencyContext.collectPromise(new Promise((resolve, reject) => {image.addEventListener('load', resolve);- image.addEventListener('error', reject);+ image.addEventListener('error', () =>+ reject(+ new DetailedError({+ message: `Failed to load an image`,+ remarks: `\+The <code>src</code> property was set to:+<pre><code>${rawSrc}</code></pre>+...which resolved to the following url:+<pre><code>${src}</code></pre>+Make sure that source is correct and that the image exists.<br/>+<a target='_blank' href='https://motioncanvas.io/docs/media#images'>Learn more</a>+about working with images.`,+ inspect: this.key,+ }),+ ),+ );}),);}diff --git a/packages/core/src/app/Logger.ts b/packages/core/src/app/Logger.tsindex 977925de..05eaa9f3 100644--- a/packages/core/src/app/Logger.ts+++ b/packages/core/src/app/Logger.ts@@ -10,13 +10,87 @@ export enum LogLevel {Silly = 'silly',}+/**+ * Represents an individual log entry.+ *+ * @remarks+ * When displayed in the editor, the log entry will have the following format:+ * ```+ * inspect node ┐+ * ┌ expand more duration ┐ │+ * ▼ ▼ ▼+ * ┌────────────────────────────────────────────┐+ * │ ▶ message 300 ms (+) │+ * ├────────────────────────────────────────────┤+ * │ remarks │+ * │ object │+ * │ stacktrace │+ * └────────────────────────────────────────────┘+ * ```+ */export interface LogPayload {+ /**+ * The log level.+ */level?: LogLevel;++ /**+ * The main message of the log.+ *+ * @remarks+ * Always visible.+ */message: string;- stack?: string;++ /**+ * Additional information about the log.+ *+ * @remarks+ * Visible only when the log is expanded.+ */remarks?: string;++ /**+ * An object that will be serialized as JSON and displayed under the message.+ *+ * @remarks+ * Visible only when the log is expanded.+ */object?: any;++ /**+ * The stack trace of the log.+ *+ * @remarks+ * Visible only when the log is expanded.+ * The current stack trace can be obtained using `new Error().stack`.+ * Both Chromium and Firefox stack traces are supported.+ */+ stack?: string;++ /**+ * An optional duration in milliseconds.+ *+ * @remarks+ * Can be used to display any duration related to the log.+ * The value is always visible next to the message.+ */durationMs?: number;++ /**+ * An optional key used to inspect a related object.+ *+ * @remarks+ * This will be used together with the {@link scenes.Inspectable} interface to+ * display additional information about the inspected object.+ * When specified, the log will have an "inspect" button that will open the+ * "Properties" tab and select the inspected object.+ */+ inspect?: string;++ /**+ * Any additional information that the log may contain.+ */[K: string]: any;}diff --git a/packages/core/src/utils/DetailedError.ts b/packages/core/src/utils/DetailedError.tsindex c32f87c7..70eee80b 100644--- a/packages/core/src/utils/DetailedError.ts+++ b/packages/core/src/utils/DetailedError.ts@@ -1,5 +1,40 @@+import type {LogPayload} from '../app';++type DetailedErrorProps = Pick<+ LogPayload,+ 'message' | 'remarks' | 'object' | 'durationMs' | 'inspect'+>;+export class DetailedError extends Error {- public constructor(message: string, public readonly remarks?: string) {- super(message);+ /**+ * {@inheritDoc app.LogPayload.message}+ */+ public readonly remarks?: string;+ /**+ * {@inheritDoc app.LogPayload.object}+ */+ public readonly object?: any;+ /**+ * {@inheritDoc app.LogPayload.durationMs}+ */+ public readonly durationMs?: number;+ /**+ * {@inheritDoc app.LogPayload.inspect}+ */+ public readonly inspect?: string;++ public constructor(message: string, remarks?: string);+ public constructor(props: DetailedErrorProps);+ public constructor(props: string | DetailedErrorProps, remarks?: string) {+ if (typeof props === 'string') {+ super(props);+ this.remarks = remarks;+ } else {+ super(props.message);+ this.remarks = props.remarks;+ this.object = props.object;+ this.durationMs = props.durationMs;+ this.inspect = props.inspect;+ }}}