Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/friendly-theme-create-access-error.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/cli-kit': patch
---

Show a friendly error when theme creation is denied by missing theme write access
21 changes: 17 additions & 4 deletions packages/cli-kit/src/public/node/themes/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,19 @@ describe('themeCreate', () => {
preferredBehaviour: expectedApiOptions,
})
})

test('throws a friendly error when access is denied by a missing theme write access scope', async () => {
vi.mocked(adminRequestDoc).mockRejectedValue(
themeAccessDeniedError(
'The user needs write_themes and an exemption from Shopify to modify themes.',
'themeCreate',
),
)

await expect(themeCreate(params, session)).rejects.toThrow(
'The authenticated account or access token is missing write_themes and an exemption from Shopify to modify themes.',
)
})
})

describe('themeUpdate', () => {
Expand Down Expand Up @@ -808,11 +821,11 @@ describe('parseThemeFileContent', () => {
})
})

function themeAccessDeniedError(requiredAccess?: string): ClientError {
function themeAccessDeniedError(requiredAccess?: string, field = 'themes'): ClientError {
const extensions = requiredAccess ? {code: 'ACCESS_DENIED', requiredAccess} : {code: 'ACCESS_DENIED'}
const message = requiredAccess
? `Access denied for themes field. Required access: ${requiredAccess}`
: 'Access denied for themes field.'
? `Access denied for ${field} field. Required access: ${requiredAccess}`
: `Access denied for ${field} field.`

return new ClientError(
{
Expand All @@ -821,7 +834,7 @@ function themeAccessDeniedError(requiredAccess?: string): ClientError {
{
message,
extensions,
path: ['themes'],
path: [field],
} as any,
],
},
Expand Down
13 changes: 11 additions & 2 deletions packages/cli-kit/src/public/node/themes/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export async function findDevelopmentThemeByName(name: string, session: AdminSes
export async function themeCreate(params: ThemeParams, session: AdminSession): Promise<Theme | undefined> {
const themeSource = params.src ?? SkeletonThemeCdn
recordEvent('theme-api:create-theme')
const {themeCreate} = await adminRequestDoc({
const {themeCreate} = await requestThemeAdminDoc({
query: ThemeCreate,
session,
variables: {
Expand Down Expand Up @@ -684,7 +684,16 @@ function getThemeAccessRequirementForAccessDeniedError(error: ClientError): stri
const requiredAccess = accessDeniedError.extensions?.requiredAccess
if (typeof requiredAccess !== 'string') return DEFAULT_THEME_ACCESS_REQUIREMENT

return requiredAccess.trim().replace(/\.$/, '') || DEFAULT_THEME_ACCESS_REQUIREMENT
return formatThemeAccessRequirement(requiredAccess)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The formatting is a little off but I don't think that's the fault of this PR:

~/r/test-theme git:(main) shopify theme dev --store <SNIP> --password <SNIP>
╭─ error ──────────────────────────────────────────────────────────────────────────────────────────╮
│                                                                                                  │
│  The authenticated account or access token is missing write_themes and an exemption from         │
│  Shopify to modify themes.                                                                       │
│              If you think that your app is eligible for an exemption and should have access to   │
│  this API,                                                                                       │
│                then you can submit an exception request [1].                                     │
│                                                                                                  │
│  Next steps                                                                                      │
│    • If you authenticated with an Admin API access token, update the app or integration that     │
│      issued the token to include the required theme access scopes, then reauthorize it or        │
│      generate a new token.                                                                       │
│    • For `theme pull`, `theme list`, and `theme info`, add the `read_themes` scope.              │
│    • For `theme push` and `theme dev`, add both the `read_themes` and `write_themes` scopes.     │
│    • If you authenticated with your Shopify account, make sure your staff or collaborator        │
│      account can access Online Store themes, then run `shopify auth logout` and try again.       │
│    • See Shopify access scopes [2].                                                              │
│                                                                                                  │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
[1] https://docs.google.com/forms/d/e/1FAIpQLSfZTB1vxFC5d1-GPdqYunWRGUoDcOheHQzfK2RoEFEHrknt5g/viewform
[2] https://shopify.dev/api/usage/access-scopes

}

function formatThemeAccessRequirement(requiredAccess: string): string {
const requirement = requiredAccess
.trim()
.replace(/\.$/, '')
.replace(/^The user needs\s+/i, '')

return requirement || DEFAULT_THEME_ACCESS_REQUIREMENT
}

function themeGid(id: number): string {
Expand Down
Loading