Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/base_transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,24 @@ export abstract class BaseTransformer<T> {
}

if (Array.isArray(unwrappedValue)) {
return new Collection([unwrappedValue, rest], this, 1, 'toObject', new RuntimeException())
return new Collection([unwrappedValue, rest], this, 1, 'toObject')
}

if (unwrappedValue === null) {
return null
}

/**
* Capturing a stack trace is expensive, so the debugging error is only
* created when the item cannot be resolved. Creating it here keeps the
* stack pointing to the transformer call site.
*/
return new Item<InstanceType<Self>, 1, 'toObject'>(
[unwrappedValue, rest],
this,
1,
'toObject',
new RuntimeException()
unwrappedValue === undefined ? new RuntimeException() : undefined
)
}

Expand Down Expand Up @@ -194,10 +199,7 @@ export abstract class BaseTransformer<T> {
metaData: Record<string, any>,
...rest: ExtractTransformerRestTypes<ConstructorParameters<Self>>
): Paginator<InstanceType<Self>, 1, 'toObject'> {
return new Paginator(
new Collection([data, rest], this, 1, 'toObject', new RuntimeException()),
metaData
)
return new Paginator(new Collection([data, rest], this, 1, 'toObject'), metaData)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/resource/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class Collection<
/**
* Private debugging error instance for troubleshooting
*/
#debuggingError: RuntimeException
#debuggingError?: RuntimeException

/**
* Type identifier for the collection resource
Expand All @@ -68,7 +68,7 @@ export class Collection<
public transformer: { new (...args: any[]): Transformer },
public maxDepth: MaxDepth,
public variant: Variant,
debuggingError: RuntimeException
debuggingError?: RuntimeException
) {
this.#debuggingError = debuggingError
}
Expand Down
10 changes: 6 additions & 4 deletions src/resource/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import type { ContainerResolver } from '@adonisjs/fold'
import type { RuntimeException } from '@poppinss/exception'
import { RuntimeException } from '@poppinss/exception'

import { transformAndResolve } from '../helpers.ts'
import type { ExtractTransformerVariants, Next, UnpackAsTopLevelItem } from '../types.ts'
Expand Down Expand Up @@ -40,7 +40,7 @@ export class Item<
/**
* Private debugging error instance for troubleshooting
*/
#debuggingError: RuntimeException
#debuggingError?: RuntimeException

/**
* Type identifier for the item resource
Expand All @@ -55,14 +55,15 @@ export class Item<
* @param transformer - Constructor for the transformer class
* @param maxDepth - Maximum depth for nested transformations
* @param variant - Variant method name to use for transformation
* @param debuggingError - Runtime exception for debugging purposes
* @param debuggingError - Error thrown when the resource is undefined. Create it at the
* transformer call site to get a useful stack trace
*/
constructor(
protected transformerData: [resource: any, ...rest: any[]],
public transformer: { new (...args: any[]): Transformer },
public maxDepth: MaxDepth,
public variant: Variant,
debuggingError: RuntimeException
debuggingError?: RuntimeException
) {
this.#debuggingError = debuggingError
}
Expand Down Expand Up @@ -153,6 +154,7 @@ export class Item<
* If its undefined after unpacking, then throw an error
*/
if (resource === undefined) {
this.#debuggingError ??= new RuntimeException()
this.#debuggingError.message =
'Cannot transform undefined value. Use "this.whenLoaded(value)" to allow undefined values'
throw this.#debuggingError
Expand Down
Loading
Loading