Skip to content

Commit d4fa172

Browse files
committed
refactor(workflow-code): name the name-taken hint, inline the one-field option check, and share the exit-code text
1 parent e811183 commit d4fa172

4 files changed

Lines changed: 20 additions & 19 deletions

File tree

‎apps/sim/lib/workspace-source/application/errors.ts‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,21 @@ export function isRefusal(error: unknown): error is OrchestrationError {
9595
)
9696
}
9797

98+
/** What to do about a name or key another resource holds, by what holds it and where it is declared. */
99+
function nameTakenHint(error: unknown, kind: WorkspaceKind): string {
100+
if (error instanceof ResourceKeyTakenError) {
101+
return 'Rename the key, or delete the resource that holds it.'
102+
}
103+
if (error instanceof WorkspaceFileFolderConflictError) {
104+
return 'Rename that folder in the file paths locally, or rename or delete the folder that holds the name in Sim.'
105+
}
106+
// A file has no declaration: its name is its path in the directory.
107+
if (kind === 'file') {
108+
return 'Rename it locally, or rename or delete the file that holds the name in Sim.'
109+
}
110+
return 'Rename it in its declaration, or rename or delete the resource that holds the name in Sim.'
111+
}
112+
98113
/**
99114
* A child use case's refusal as a finding about one resource: a resource (or a table's rows)
100115
* changed since the plan read it is `E_APPLY_STALE`, forbidden is `E_APPLY_FORBIDDEN`,
@@ -131,14 +146,7 @@ export function refusalDiagnostic(
131146
}
132147
if (error instanceof ResourceKeyTakenError || isNameTaken(error)) {
133148
return finding('E_NAME_TAKEN', error.message, {
134-
hint:
135-
error instanceof ResourceKeyTakenError
136-
? 'Rename the key, or delete the resource that holds it.'
137-
: error instanceof WorkspaceFileFolderConflictError
138-
? 'Rename that folder in the file paths locally, or rename or delete the folder that holds the name in Sim.'
139-
: resource.kind === 'file'
140-
? 'Rename it locally, or rename or delete the file that holds the name in Sim.'
141-
: 'Rename it in its declaration, or rename or delete the resource that holds the name in Sim.',
149+
hint: nameTakenHint(error, resource.kind),
142150
...resource,
143151
})
144152
}

‎packages/sim-cli/src/commands/protocol/source-check.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -560,13 +560,9 @@ export async function checkWorkflowDir(
560560
if (finding.code !== 'W_UNKNOWN_OPTION' || blockKey === undefined || param === undefined) {
561561
return false
562562
}
563-
return optionsVaryOn(blockKey, (id) => id === param)
564-
}
565-
/** Whether a field of block `blockKey` that `matches` takes options varying by deployment. */
566-
const optionsVaryOn = (blockKey: string, matches: (subBlockId: string) => boolean) => {
567563
const type = compiled.workflow ? getOwn(compiled.workflow.blocks, blockKey)?.type : undefined
568564
const block = type === undefined ? undefined : catalogBlock(compiledDir.catalog, type)
569-
return block?.subBlocks.some((sub) => matches(sub.id) && sub.optionsVary === true) === true
565+
return block?.subBlocks.some((sub) => sub.id === param && sub.optionsVary === true) === true
570566
}
571567
const exempt = (finding: (typeof compiled.diagnostics)[number]) =>
572568
mayBeRenamed(finding) || variesByDeployment(finding)

‎packages/sim-cli/src/commands/protocol/source-exit-codes.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,7 @@ function sourceExitCodeRows(prefix: SourceCommandGroup): SourceExitCodeRow[] {
6969
},
7070
{
7171
code: 1,
72-
meaning:
73-
prefix === 'sim workspaces source'
74-
? 'The source has errors (each printed as `file:line:column` with a hint), a locked block (`E_APPLY_LOCKED_BLOCK`), a name another resource holds (`E_NAME_TAKEN`), a cancelled prompt, or the command failed'
75-
: 'The source has errors (each printed as `file:line:column` with a hint), a locked block (`E_APPLY_LOCKED_BLOCK`), a name another workflow holds (`SOURCE_NAME_TAKEN`), a cancelled prompt, or the command failed',
72+
meaning: `The source has errors (each printed as \`file:line:column\` with a hint), a locked block (\`E_APPLY_LOCKED_BLOCK\`), ${prefix === 'sim workspaces source' ? 'a name another resource holds (`E_NAME_TAKEN`)' : 'a name another workflow holds (`SOURCE_NAME_TAKEN`)'}, a cancelled prompt, or the command failed`,
7673
next: 'Fix what it printed and run it again',
7774
help: {
7875
'workflows source apply':

‎packages/workflow-code/src/diagnostics/refusals.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ const CHANGED_BLOCK_REFUSALS: ReadonlyMap<string, EditRefusalCode> = new Map([
9898
* changed: a dropdown value none of its options names, which the server's field gate refuses. A
9999
* value the stored block already holds outside its options prints as `raw()`, which the compile
100100
* does not check, and one written bare again is not a field the edit set, so only a value the
101-
* author changed is refused. `operation` is left out: an unknown one is already an error, and its warning means the
102-
* stored block holds a retired operation, which the gate keeps.
101+
* author changed is refused. `operation` is left out: an unknown one is already an error, and its
102+
* warning means the stored block holds a retired operation, which the gate keeps.
103103
*/
104104
const PARAM_REFUSALS: ReadonlyMap<string, EditRefusalCode> = new Map([
105105
['W_UNKNOWN_OPTION', 'E_APPLY_INVALID_SUBBLOCK_FIELD'],

0 commit comments

Comments
 (0)