Unverified Commit 3a5d293c authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

feat(turbopack): Log anonymized internal error (panic) information to telemetry (#81272)

## Goals

- Pass through a custom `TurbopackInternalError` type when a Rust panic or an unexpected top-level `anyhow::Error` occurs.
- Make a best-effort attempt to log a location of the error to telemetry.
  - In the case of a panic, we should have the file, line, and column where the panic occured. This could be obscured inside of a helper method, but it's better than nothing. Since this is a position in Turbopack's source code, there's zero risk of user data being included here, so it should be safe to log to telemetry.

## How it works

In previous PRs, @wbinnssmith did the following:
- Added a thread-local for storing the most recent panic information in Rust
- Added `catch_unwind` logic to intercept the panic in turbo-tasks and read the thread-local information, with care take to make sure we're reading from the same thread (not across an `await` boundary).
- Added a custom `std::error::Error` type for internal errors, with support for storing information about a source panic.
- Ensured we set up `Error::source` correctly everywhere so that you can trace an error back to the root cause (presumably a panic).

In #80881, I added a `NextTurbopackContext` type that we can extend to store additional helpers on.

In this PR:
- Adds a callback to the `Project` constructor called `throwTurbopackInternalError` that constructs and throws a custom `TurbopackInternalError`. The definition of `TurbopackInternalError` must be in JS, as we cannot reasonably define the error subclass using NAPI.
- Wraps the callback in a `ThreadsafeFunction` so that it can be called in any of our async functions.
- Replaces all the places we were previously converting to `napi::Error` with logic to run `throwTurbopackInternalError`.

In a subsequent PR, we could move more of the panic handling into JS using `throwTurbopackInternalError`.

## Testing

Added a panic and ran:

```
NEXT_TELEMETRY_DEBUG=1 pnpm next dev --turbopack examples/basic-css/
```

<img src="https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/HAZVitxRNnZz8QMiPn4a/7f261d3f-351e-436a-9345-fd6e9056d20c.png" width="600">
parent f8afa172
......@@ -2321,9 +2321,9 @@ checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c"
[[package]]
name = "futures"
version = "0.3.30"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0"
checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876"
dependencies = [
"futures-channel",
"futures-core",
......@@ -2352,9 +2352,9 @@ checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
[[package]]
name = "futures-executor"
version = "0.3.30"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d"
checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f"
dependencies = [
"futures-core",
"futures-task",
......@@ -4415,6 +4415,7 @@ dependencies = [
"console-subscriber",
"dhat",
"either",
"futures-util",
"getrandom 0.2.15",
"iana-time-zone",
"lightningcss-napi",
......
......@@ -349,7 +349,8 @@ dhat = { version = "0.3.2" }
dunce = "1.0.3"
either = "1.9.0"
erased-serde = "0.4.5"
futures = "0.3.26"
futures = "0.3.31"
futures-util = "0.3.31"
futures-retry = "0.6.0"
hashbrown = "0.14.5"
image = { version = "0.25.0", default-features = false }
......
......@@ -58,6 +58,7 @@ anyhow = "1.0.66"
console-subscriber = { workspace = true, optional = true }
dhat = { workspace = true, optional = true }
either = { workspace = true }
futures-util = { workspace = true }
owo-colors = { workspace = true }
napi = { workspace = true }
napi-derive = "2"
......
use std::{ops::Deref, sync::Arc};
use anyhow::Result;
use futures_util::TryFutureExt;
use napi::{JsFunction, bindgen_prelude::External};
use next_api::{
operation::OptionEndpoint,
......@@ -12,7 +13,7 @@ use next_api::{
};
use tracing::Instrument;
use turbo_tasks::{Completion, Effects, OperationVc, ReadRef, Vc};
use turbopack_core::{diagnostics::PlainDiagnostic, error::PrettyPrintError, issue::PlainIssue};
use turbopack_core::{diagnostics::PlainDiagnostic, issue::PlainIssue};
use super::utils::{
DetachedVc, NapiDiagnostic, NapiIssue, RootTask, TurbopackResult,
......@@ -125,6 +126,7 @@ async fn get_written_endpoint_with_issues_operation(
pub async fn endpoint_write_to_disk(
#[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: External<ExternalEndpoint>,
) -> napi::Result<TurbopackResult<NapiWrittenEndpoint>> {
let ctx = endpoint.turbopack_ctx();
let endpoint_op = ***endpoint;
let (written, issues, diags) = endpoint
.turbopack_ctx()
......@@ -144,8 +146,8 @@ pub async fn endpoint_write_to_disk(
Ok((written.clone(), issues.clone(), diagnostics.clone()))
})
.await
.map_err(|e| napi::Error::from_reason(PrettyPrintError(&e).to_string()))?;
.or_else(|e| ctx.throw_turbopack_internal_result(&e))
.await?;
Ok(TurbopackResult {
result: NapiWrittenEndpoint::from(written.map(ReadRef::into_owned)),
issues: issues.iter().map(|i| NapiIssue::from(&**i)).collect(),
......
This diff is collapsed.
This diff is collapsed.
use std::{future::Future, ops::Deref, sync::Arc};
use anyhow::{Context, Result, anyhow};
use futures_util::TryFutureExt;
use napi::{
JsFunction, JsObject, JsUnknown, NapiRaw, NapiValue, Status,
bindgen_prelude::{External, ToNapiValue},
......@@ -14,14 +15,13 @@ use turbo_tasks::{
use turbo_tasks_fs::FileContent;
use turbopack_core::{
diagnostics::{Diagnostic, DiagnosticContextExt, PlainDiagnostic},
error::PrettyPrintError,
issue::{
IssueDescriptionExt, IssueSeverity, PlainIssue, PlainIssueSource, PlainSource, StyledString,
},
source_pos::SourcePos,
};
use crate::{next_api::turbopack_ctx::NextTurbopackContext, util::log_internal_error_and_inform};
use crate::next_api::turbopack_ctx::NextTurbopackContext;
/// An [`OperationVc`] that can be passed back and forth to JS across the [`napi`][mod@napi]
/// boundary via [`External`].
......@@ -324,35 +324,35 @@ impl<T: ToNapiValue> ToNapiValue for TurbopackResult<T> {
}
pub fn subscribe<T: 'static + Send + Sync, F: Future<Output = Result<T>> + Send, V: ToNapiValue>(
turbopack_ctx: NextTurbopackContext,
ctx: NextTurbopackContext,
func: JsFunction,
handler: impl 'static + Sync + Send + Clone + Fn() -> F,
mapper: impl 'static + Sync + Send + FnMut(ThreadSafeCallContext<T>) -> napi::Result<Vec<V>>,
) -> napi::Result<External<RootTask>> {
let func: ThreadsafeFunction<T> = func.create_threadsafe_function(0, mapper)?;
let task_id = turbopack_ctx.turbo_tasks().spawn_root_task(move || {
let handler = handler.clone();
let func = func.clone();
Box::pin(async move {
let result = handler().await;
let status = func.call(
result.map_err(|e| {
log_internal_error_and_inform(&e);
napi::Error::from_reason(PrettyPrintError(&e).to_string())
}),
ThreadsafeFunctionCallMode::NonBlocking,
);
if !matches!(status, Status::Ok) {
let error = anyhow!("Error calling JS function: {}", status);
eprintln!("{error}");
return Err::<Vc<()>, _>(error);
let task_id = ctx.turbo_tasks().spawn_root_task({
let ctx = ctx.clone();
move || {
let ctx = ctx.clone();
let handler = handler.clone();
let func = func.clone();
async move {
let result = handler()
.or_else(|e| ctx.throw_turbopack_internal_result(&e))
.await;
let status = func.call(result, ThreadsafeFunctionCallMode::NonBlocking);
if !matches!(status, Status::Ok) {
let error = anyhow!("Error calling JS function: {}", status);
eprintln!("{error}");
return Err::<Vc<()>, _>(error);
}
Ok(Default::default())
}
Ok(Default::default())
})
}
});
Ok(External::new(RootTask {
turbopack_ctx,
turbopack_ctx: ctx,
task_id: Some(task_id),
}))
}
......
......@@ -26,140 +26,12 @@ IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
use std::{
cell::RefCell,
env,
fs::OpenOptions,
io::{self, BufRead, Write},
path::PathBuf,
sync::Mutex,
time::Instant,
};
use std::{cell::RefCell, env, path::PathBuf};
use anyhow::anyhow;
use napi::bindgen_prelude::{External, Status};
use once_cell::sync::Lazy;
use owo_colors::OwoColorize;
use terminal_hyperlink::Hyperlink;
use tracing_chrome::{ChromeLayerBuilder, FlushGuard};
use tracing_subscriber::{Layer, filter, prelude::*, util::SubscriberInitExt};
use turbopack_core::error::PrettyPrintError;
static LOG_THROTTLE: Mutex<Option<Instant>> = Mutex::new(None);
static LOG_DIVIDER: &str = "---------------------------";
static PANIC_LOG: Lazy<PathBuf> = Lazy::new(|| {
let mut path = env::temp_dir();
path.push(format!("next-panic-{:x}.log", rand::random::<u128>()));
path
});
pub fn log_internal_error_and_inform(internal_error: &anyhow::Error) {
if cfg!(debug_assertions)
|| env::var("SWC_DEBUG") == Ok("1".to_string())
|| env::var("CI").is_ok_and(|v| !v.is_empty())
// Next's run-tests unsets CI and sets NEXT_TEST_CI
|| env::var("NEXT_TEST_CI").is_ok_and(|v| !v.is_empty())
{
eprintln!(
"{}: An unexpected Turbopack error occurred:\n{}",
"FATAL".red().bold(),
PrettyPrintError(internal_error)
);
return;
}
// hold open this mutex guard to prevent concurrent writes to the file!
let mut last_error_time = LOG_THROTTLE.lock().unwrap();
if let Some(last_error_time) = last_error_time.as_ref()
&& last_error_time.elapsed().as_secs() < 1
{
// Throttle panic logging to once per second
return;
}
*last_error_time = Some(Instant::now());
let size = std::fs::metadata(PANIC_LOG.as_path()).map(|m| m.len());
if let Ok(size) = size
&& size > 512 * 1024
{
// Truncate the earliest error from log file if it's larger than 512KB
let new_lines = {
let log_read = OpenOptions::new()
.read(true)
.open(PANIC_LOG.as_path())
.unwrap_or_else(|_| panic!("Failed to open {}", PANIC_LOG.to_string_lossy()));
io::BufReader::new(&log_read)
.lines()
.skip(1)
.skip_while(|line| match line {
Ok(line) => !line.starts_with(LOG_DIVIDER),
Err(_) => false,
})
.collect::<Vec<_>>()
};
let mut log_write = OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(PANIC_LOG.as_path())
.unwrap_or_else(|_| panic!("Failed to open {}", PANIC_LOG.to_string_lossy()));
for line in new_lines {
match line {
Ok(line) => {
writeln!(log_write, "{line}").unwrap();
}
Err(_) => {
break;
}
}
}
}
let mut log_file = OpenOptions::new()
.create(true)
.append(true)
.open(PANIC_LOG.as_path())
.unwrap_or_else(|_| panic!("Failed to open {}", PANIC_LOG.to_string_lossy()));
let internal_error_str: String = PrettyPrintError(internal_error).to_string();
writeln!(log_file, "{}\n{}", LOG_DIVIDER, &internal_error_str).unwrap();
let title = format!(
"Turbopack Error: {}",
internal_error_str.lines().next().unwrap_or("Unknown")
);
let version_str = format!(
"Turbopack version: `{}`\nNext.js version: `{}`",
env!("VERGEN_GIT_DESCRIBE"),
env!("NEXTJS_VERSION")
);
let new_discussion_url = if supports_hyperlinks::supports_hyperlinks() {
"clicking here.".hyperlink(
format!(
"https://github.com/vercel/next.js/discussions/new?category=turbopack-error-report&title={}&body={}&labels=Turbopack,Turbopack%20Panic%20Backtrace",
&urlencoding::encode(&title),
&urlencoding::encode(&format!("{}\n\nError message:\n```\n{}\n```", &version_str, &internal_error_str))
)
)
} else {
format!(
"clicking here: https://github.com/vercel/next.js/discussions/new?category=turbopack-error-report&title={}&body={}&labels=Turbopack,Turbopack%20Panic%20Backtrace",
&urlencoding::encode(&title),
&urlencoding::encode(&format!("{}\n\nError message:\n```\n{}\n```", &version_str, &title))
)
};
eprintln!(
"\n-----\n{}: An unexpected Turbopack error occurred. A panic log has been written to \
{}.\n\nTo help make Turbopack better, report this error by {}\n-----\n",
"FATAL".red().bold(),
PANIC_LOG.to_string_lossy(),
&new_discussion_url
);
}
use tracing_subscriber::{Layer, filter, layer::SubscriberExt, util::SubscriberInitExt};
#[napi]
pub fn get_target_triple() -> &'static str {
......
......@@ -720,5 +720,6 @@
"719": "Failed to get source map for '%s'. This is a bug in Next.js",
"720": "A client prerender store should not be used for a route handler.",
"721": "Render in Browser",
"722": "Unable to match pathname to a dynamic route"
"722": "Unable to match pathname to a dynamic route",
"723": "NAPI type conversion error in throwTurbopackInternalError"
}
......@@ -204,7 +204,8 @@ export interface NapiTurboEngineOptions {
}
export declare function projectNew(
options: NapiProjectOptions,
turboEngineOptions: NapiTurboEngineOptions
turboEngineOptions: NapiTurboEngineOptions,
napiCallbacks: NapiNextTurbopackCallbacksJsObject
): Promise<{ __napiType: 'Project' }>
export declare function projectUpdate(
project: { __napiType: 'Project' },
......@@ -350,6 +351,31 @@ export declare function projectGetSourceMapSync(
project: { __napiType: 'Project' },
filePath: RcStr
): string | null
/**
* A version of [`NapiNextTurbopackCallbacks`] that can accepted as an argument to a napi function.
*
* This can be converted into a [`NapiNextTurbopackCallbacks`] with
* [`NapiNextTurbopackCallbacks::from_js`].
*/
export interface NapiNextTurbopackCallbacksJsObject {
/**
* Called when we've encountered a bug in Turbopack and not in the user's code. Constructs and
* throws a `TurbopackInternalError` type. Logs to anonymized telemetry.
*
* As a result of the use of `ErrorStrategy::CalleeHandled`, the first argument is an error if
* there's a runtime conversion error. This should never happen, but if it does, the function
* can throw it instead.
*/
throwTurbopackInternalError: (
conversionError: Error | null,
opts: TurbopackInternalErrorOpts
) => never
}
/** Arguments for [`NapiNextTurbopackCallbacks::throw_turbopack_internal_error`]. */
export interface TurbopackInternalErrorOpts {
message: string
anonymizedLocation?: string
}
export declare function rootTaskDispose(rootTask: {
__napiType: 'RootTask'
}): void
......
......@@ -3,11 +3,11 @@
/* tslint:disable */
/* eslint-disable */
export function mdxCompileSync(value: string, opts: any): any
export function mdxCompile(value: string, opts: any): Promise<any>
export function minifySync(s: string, opts: any): any
export function minify(s: string, opts: any): Promise<any>
export function transformSync(s: any, opts: any): any
export function transform(s: any, opts: any): Promise<any>
export function parseSync(s: string, opts: any): any
export function parse(s: string, opts: any): Promise<any>
export function mdxCompileSync(value: string, opts: any): any
export function mdxCompile(value: string, opts: any): Promise<any>
......@@ -40,7 +40,7 @@ import type {
UpdateMessage,
WrittenEndpoint,
} from './types'
import { TurbopackInternalError } from '../../shared/lib/turbopack/utils'
import { throwTurbopackInternalError } from '../../shared/lib/turbopack/internal-error'
type RawBindings = typeof import('./generated-native')
type RawWasmBindings = typeof import('./generated-wasm') & {
......@@ -536,14 +536,6 @@ function bindingToApi(
throw new Error(`Invariant: ${computeMessage(never)}`)
}
async function withErrorCause<T>(fn: () => Promise<T>): Promise<T> {
try {
return await fn()
} catch (nativeError: any) {
throw TurbopackInternalError.createAndRecordTelemetry(nativeError)
}
}
/**
* Calls a native function and streams the result.
* If useBuffer is true, all values will be preserved, potentially buffered
......@@ -589,9 +581,7 @@ function bindingToApi(
}
async function* createIterator() {
const task = await withErrorCause<{ __napiType: 'RootTask' } | void>(() =>
nativeFunction(emitResult)
)
const task = await nativeFunction(emitResult)
try {
while (!canceled) {
if (buffer.length > 0) {
......@@ -607,9 +597,6 @@ function bindingToApi(
}
} catch (e) {
if (e === cancel) return
if (e instanceof Error) {
throw TurbopackInternalError.createAndRecordTelemetry(e)
}
throw e
} finally {
if (task) {
......@@ -662,25 +649,21 @@ function bindingToApi(
}
async update(options: Partial<ProjectOptions>) {
await withErrorCause(async () =>
binding.projectUpdate(
this._nativeProject,
await rustifyPartialProjectOptions(options)
)
await binding.projectUpdate(
this._nativeProject,
await rustifyPartialProjectOptions(options)
)
}
async writeAllEntrypointsToDisk(
appDirOnly: boolean
): Promise<TurbopackResult<RawEntrypoints>> {
return await withErrorCause(async () => {
const napiEndpoints = (await binding.projectWriteAllEntrypointsToDisk(
this._nativeProject,
appDirOnly
)) as TurbopackResult<NapiEntrypoints>
const napiEndpoints = (await binding.projectWriteAllEntrypointsToDisk(
this._nativeProject,
appDirOnly
)) as TurbopackResult<NapiEntrypoints>
return napiEntrypointsToRawEntrypoints(napiEndpoints)
})
return napiEntrypointsToRawEntrypoints(napiEndpoints)
}
entrypointsSubscribe() {
......@@ -777,22 +760,16 @@ function bindingToApi(
}
async writeToDisk(): Promise<TurbopackResult<WrittenEndpoint>> {
return await withErrorCause(
() =>
binding.endpointWriteToDisk(this._nativeEndpoint) as Promise<
TurbopackResult<WrittenEndpoint>
>
)
return (await binding.endpointWriteToDisk(
this._nativeEndpoint
)) as TurbopackResult<WrittenEndpoint>
}
async clientChanged(): Promise<AsyncIterableIterator<TurbopackResult<{}>>> {
const clientSubscription = subscribe<TurbopackResult>(
false,
async (callback) =>
binding.endpointClientChangedSubscribe(
await this._nativeEndpoint,
callback
)
binding.endpointClientChangedSubscribe(this._nativeEndpoint, callback)
)
await clientSubscription.next()
return clientSubscription
......@@ -805,7 +782,7 @@ function bindingToApi(
false,
async (callback) =>
binding.endpointServerChangedSubscribe(
await this._nativeEndpoint,
this._nativeEndpoint,
includeIssues,
callback
)
......@@ -1085,7 +1062,10 @@ function bindingToApi(
return new ProjectImpl(
await binding.projectNew(
await rustifyProjectOptions(options),
turboEngineOptions || {}
turboEngineOptions || {},
{
throwTurbopackInternalError,
}
)
)
}
......
......@@ -80,9 +80,9 @@ import { store as consoleStore } from '../../../build/output/store'
import {
isPersistentCachingEnabled,
ModuleBuildError,
TurbopackInternalError,
} from '../../../shared/lib/turbopack/utils'
import { getDefineEnv } from '../../../build/define-env'
import { TurbopackInternalError } from '../../../shared/lib/turbopack/internal-error'
export type SetupOpts = {
renderServer: LazyRenderServerInstance
......
import type { TurbopackInternalErrorOpts } from '../../../build/swc/generated-native'
import { eventErrorThrown } from '../../../telemetry/events'
import { traceGlobals } from '../../../trace/shared'
/**
* An error caused by a bug in Turbopack, and not the user's code (e.g. a Rust panic). These should
* be written to a log file and details should not be shown to the user.
*
* These are constructed in Turbopack by calling `throwTurbopackInternalError`.
*/
export class TurbopackInternalError extends Error {
name = 'TurbopackInternalError'
location: string | undefined
// Manually set this as this isn't statically determinable
__NEXT_ERROR_CODE = 'TurbopackInternalError'
constructor({ message, anonymizedLocation }: TurbopackInternalErrorOpts) {
super(message)
this.location = anonymizedLocation
}
}
/**
* A helper used by the napi Rust entrypoints to construct and throw a `TurbopackInternalError`.
*
* When called, this will emit a telemetry event.
*/
export function throwTurbopackInternalError(
conversionError: Error | null,
opts: TurbopackInternalErrorOpts
): never {
if (conversionError != null) {
// Somehow napi failed to convert `opts` to a JS object??? Just give up and throw that instead.
throw new Error(
'NAPI type conversion error in throwTurbopackInternalError',
{
cause: conversionError,
}
)
}
const err = new TurbopackInternalError(opts)
const telemetry = traceGlobals.get('telemetry')
if (telemetry) {
telemetry.record(eventErrorThrown(err, opts.anonymizedLocation))
} else {
console.error('Expected `telemetry` to be set in globals')
}
throw err
}
......@@ -15,47 +15,20 @@ import type { EntryKey } from './entry-key'
import * as Log from '../../../build/output/log'
import type { NextConfigComplete } from '../../../server/config-shared'
import loadJsConfig from '../../../build/load-jsconfig'
import { eventErrorThrown } from '../../../telemetry/events'
import { traceGlobals } from '../../../trace/shared'
type IssueKey = `${Issue['severity']}-${Issue['filePath']}-${string}-${string}`
export type IssuesMap = Map<IssueKey, Issue>
export type EntryIssuesMap = Map<EntryKey, IssuesMap>
export type TopLevelIssuesMap = IssuesMap
// An error generated from emitted Turbopack issues. This can include build
// errors caused by issues with user code.
/**
* An error generated from emitted Turbopack issues. This can include build
* errors caused by issues with user code.
*/
export class ModuleBuildError extends Error {
name = 'ModuleBuildError'
}
// An error caused by an internal issue in Turbopack. These should be written
// to a log file and details should not be shown to the user.
export class TurbopackInternalError extends Error {
name = 'TurbopackInternalError'
// Manually set this as this isn't statically determinable
__NEXT_ERROR_CODE = 'TurbopackInternalError'
static createAndRecordTelemetry(cause: Error) {
const error = new TurbopackInternalError(cause)
const telemetry = traceGlobals.get('telemetry')
if (telemetry) {
telemetry.record(eventErrorThrown(error))
} else {
console.error('Expected `telemetry` to be set in globals')
}
return error
}
constructor(cause: Error) {
super(cause.message)
this.stack = cause.stack
}
}
/**
* Thin stopgap workaround layer to mimic existing wellknown-errors-plugin in webpack's build
* to emit certain type of errors into cli.
......
......@@ -235,15 +235,24 @@ type ErrorThrownEvent = {
eventName: typeof ERROR_THROWN_EVENT
payload: {
errorCode: string | undefined
location: string | undefined
}
}
// Creates a Telemetry event for errors. For privacy, only includes the error code.
export function eventErrorThrown(error: Error): ErrorThrownEvent {
// Creates a Telemetry event for errors. For privacy, only includes the error code and not the error
// message.
//
// `location` may be included if it's a location internal to the next.js source tree (i.e. a
// non-absolute path).
export function eventErrorThrown(
error: Error,
anonymizedLocation: string | undefined
): ErrorThrownEvent {
return {
eventName: ERROR_THROWN_EVENT,
payload: {
errorCode: extractNextErrorCode(error) || 'Unknown',
location: anonymizedLocation,
},
}
}
import path from 'path'
import os from 'os'
import { TurbopackInternalError } from 'next/dist/shared/lib/turbopack/utils'
import {
throwTurbopackInternalError,
TurbopackInternalError,
} from 'next/dist/shared/lib/turbopack/internal-error'
import { Telemetry } from 'next/dist/telemetry/storage'
import { setGlobal } from 'next/dist/trace'
import { traceGlobals } from 'next/dist/trace/shared'
describe('TurbopackInternalError', () => {
it('sends a telemetry event when TurbopackInternalError.createAndRecordTelemetry() is called', async () => {
it('sends a telemetry event when throwTurbopackInternalError() is called', async () => {
const oldTelemetry = traceGlobals.get('telemetry')
try {
......@@ -18,12 +21,24 @@ describe('TurbopackInternalError', () => {
.spyOn(telemetry, 'submitRecord')
// @ts-ignore
.mockImplementation(() => Promise.resolve())
TurbopackInternalError.createAndRecordTelemetry(new Error('test error'))
let internalError = null
try {
throwTurbopackInternalError(null, {
message: 'test error',
anonymizedLocation: 'file.rs:120:1',
})
} catch (err) {
internalError = err
}
expect(internalError).toBeInstanceOf(TurbopackInternalError)
expect(submitRecord).toHaveBeenCalledWith({
eventName: 'NEXT_ERROR_THROWN',
payload: {
errorCode: 'TurbopackInternalError',
location: 'file.rs:120:1',
},
})
} finally {
......
......@@ -447,7 +447,7 @@ pub enum TurboTasksExecutionError {
}
impl TurboTasksExecutionError {
pub fn task_context(&self, task: impl Display) -> Self {
pub fn with_task_context(&self, task: impl Display) -> Self {
TurboTasksExecutionError::TaskContext(Arc::new(TurboTaskContextError {
task: RcStr::from(task.to_string()),
source: Some(self.clone()),
......
......@@ -831,7 +831,7 @@ impl<B: Backend + 'static> TurboTasks<B> {
let local_task = LocalTask::Done {
output: match result {
Ok(raw_vc) => OutputContent::Link(raw_vc),
Err(err) => OutputContent::Error(err.task_context(task_type)),
Err(err) => OutputContent::Error(err.with_task_context(task_type)),
},
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment