Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render error page on theme asset upload error #5221

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions .changeset/big-hairs-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@shopify/cli-kit': minor
'@shopify/theme': minor
---

Store theme asset upload errors encountered while running the theme dev command
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
interface Error {
message: string
code: string
}

export function getErrorPage(options: {title: string; header: string; errors: Error[]}) {
const html = String.raw

return html`<html>
<head>
<title>${options.title ?? 'Unknown error'}</title>
</head>
<body
id="full-error-page"
style="display: flex; flex-direction: column; align-items: center; padding-top: 20px; font-family: Arial"
>
<h1>${options.header}</h1>

${options.errors
.map(
(error) =>
`<p>${error.message}</p>
<pre>${error.code}</pre>`,
)
.join('')}
</body>
</html>`
}
38 changes: 18 additions & 20 deletions packages/theme/src/cli/utilities/theme-environment/html.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {getProxyStorefrontHeaders, patchRenderingResponse} from './proxy.js'
import {getInMemoryTemplates, injectHotReloadScript} from './hot-reload/server.js'
import {render} from './storefront-renderer.js'
import {getErrorPage} from './hot-reload/error-page.js'
import {getExtensionInMemoryTemplates} from '../theme-ext-environment/theme-ext-server.js'
import {logRequestLine} from '../log-request-line.js'
import {defineEventHandler, getCookie, setResponseHeader, setResponseStatus, type H3Error} from 'h3'
Expand Down Expand Up @@ -31,6 +32,17 @@ export function getHtmlHandler(theme: Theme, ctx: DevServerContext) {

assertThemeId(response, html, String(theme.id))

if (ctx.localThemeFileSystem.uploadErrors.size > 0) {
html = getErrorPage({
title: 'Failed to Upload Theme Files',
header: 'Upload Errors',
errors: Array.from(ctx.localThemeFileSystem.uploadErrors.entries()).map(([file, errors]) => ({
message: file,
code: errors.join('\n'),
})),
})
}

if (ctx.options.liveReload !== 'off') {
html = injectHotReloadScript(html)
}
Expand All @@ -52,8 +64,12 @@ export function getHtmlHandler(theme: Theme, ctx: DevServerContext) {
let errorPageHtml = getErrorPage({
title,
header: title,
message: [...rest, cause?.message ?? error.message].join('<br>'),
code: error.stack?.replace(`${error.message}\n`, '') ?? '',
errors: [
{
message: [...rest, cause?.message ?? error.message].join('<br>'),
code: error.stack?.replace(`${error.message}\n`, '') ?? '',
},
],
})

if (ctx.options.liveReload !== 'off') {
Expand All @@ -65,24 +81,6 @@ export function getHtmlHandler(theme: Theme, ctx: DevServerContext) {
})
}

function getErrorPage(options: {title: string; header: string; message: string; code: string}) {
const html = String.raw

return html`<html>
<head>
<title>${options.title ?? 'Unknown error'}</title>
</head>
<body
id="full-error-page"
style="display: flex; flex-direction: column; align-items: center; padding-top: 20px; font-family: Arial"
>
<h2>${options.header}</h2>
<p>${options.message}</p>
<pre>${options.code}</pre>
</body>
</html>`
}

function assertThemeId(response: Response, html: string, expectedThemeId: string) {
/**
* DOM example:
Expand Down
Loading