Skip to content

Commit

Permalink
[preview] Always run preview prepare in try/catch (#1364)
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Jun 12, 2019
1 parent a8a17a6 commit 260db28
Showing 1 changed file with 3 additions and 20 deletions.
23 changes: 3 additions & 20 deletions packages/@sanity/preview/src/prepareForPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ const identity = v => v
const PRESERVE_KEYS = ['_id', '_type', '_upload']
const EMPTY = []

type __DEV__ = boolean

type ViewOptions = {}

type SelectedValue = {}
Expand Down Expand Up @@ -70,8 +68,7 @@ const reportErrors = debounce(() => {
console.groupCollapsed(
`%cHeads up! Got ${
errorCount === 1 ? 'error' : `${errorCount} errors`
} while preparing data for preview. Click for details.` +
' This may be a hard failure in production and cause your Studio to crash.',
} while preparing data for preview. Click for details.`,
'color: #ff7e7c'
)

Expand Down Expand Up @@ -194,7 +191,7 @@ function validateReturnedPreview(result: PrepareInvocationResult) {
}
}

function invokePrepareChecked(
export function invokePrepare(
type: Type,
value: SelectedValue,
viewOptions: ViewOptions
Expand All @@ -208,25 +205,11 @@ function invokePrepareChecked(
} catch (error) {
return {
returnValue: null,
errors: [assignType(error, 'prepareError')]
errors: [assignType('prepareError', error)]
}
}
}

function invokePrepareUnchecked(
type: Type,
value: SelectedValue,
viewOptions: ViewOptions
): PrepareInvocationResult {
return {
selectedValue: value,
returnValue: (type.preview.prepare || identity)(value, viewOptions),
errors: EMPTY
}
}

export const invokePrepare = __DEV__ ? invokePrepareChecked : invokePrepareUnchecked

function withErrors(result, type, selectedValue) {
result.errors.forEach(error => errorCollector.add(type, selectedValue, error))
reportErrors()
Expand Down

0 comments on commit 260db28

Please sign in to comment.