refactor(apiproxy): one wording for a preset failure on both paths
Session create and the preset switch can be handed the same two failures, and a client branching on the code needs them worded identically from either.
This commit is contained in:
@@ -198,6 +198,35 @@ function err<T>(request: RpcRequest<unknown>, error: RpcError): RpcResponse<T> {
|
|||||||
return { rpcId: request.rpcId, result: { ok: false, error } }
|
return { rpcId: request.rpcId, result: { ok: false, error } }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The RPC refusal a preset failure becomes, or undefined when the failure is
|
||||||
|
* about something else.
|
||||||
|
*
|
||||||
|
* Both the session-create path and the switch path can be handed the same two
|
||||||
|
* failures, and a client that has to branch on the code needs them worded the
|
||||||
|
* same from either.
|
||||||
|
* @param request - the request being answered.
|
||||||
|
* @param error - the thrown value.
|
||||||
|
* @returns the refusal, or undefined when the caller should keep handling.
|
||||||
|
*/
|
||||||
|
function presetFailure(request: RpcRequest<unknown>, error: unknown): RpcResponse<never> | undefined {
|
||||||
|
if (error instanceof UnknownPresetError) {
|
||||||
|
return err(request, {
|
||||||
|
code: 'agent-preset-not-found',
|
||||||
|
message: error.message,
|
||||||
|
details: { agentPreset: error.presetId, available: [...error.available] },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if (error instanceof PresetMountError) {
|
||||||
|
return err(request, {
|
||||||
|
code: 'agent-preset-invalid',
|
||||||
|
message: error.message,
|
||||||
|
details: { agentPreset: error.presetId, reason: error.reason },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
|
||||||
/** Simple async queue: core callbacks push, the AsyncIterable pulls; abort/return cleans up. */
|
/** Simple async queue: core callbacks push, the AsyncIterable pulls; abort/return cleans up. */
|
||||||
class FrameQueue<F> {
|
class FrameQueue<F> {
|
||||||
private buffer: F[] = []
|
private buffer: F[] = []
|
||||||
@@ -1771,20 +1800,8 @@ export function createApiProxy(ctx: Context, defaults: ApiProxyDefaults): ApiPro
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if (error instanceof UnknownPresetError) {
|
const refused = presetFailure(request, error)
|
||||||
return err(request, {
|
if (refused !== undefined) return refused
|
||||||
code: 'agent-preset-not-found',
|
|
||||||
message: error.message,
|
|
||||||
details: { agentPreset: error.presetId, available: [...error.available] },
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if (error instanceof PresetMountError) {
|
|
||||||
return err(request, {
|
|
||||||
code: 'agent-preset-invalid',
|
|
||||||
message: error.message,
|
|
||||||
details: { agentPreset: error.presetId, reason: error.reason },
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if (error instanceof SessionCwdConflict) {
|
if (error instanceof SessionCwdConflict) {
|
||||||
return err(request, {
|
return err(request, {
|
||||||
code: 'session-conflict',
|
code: 'session-conflict',
|
||||||
@@ -2595,20 +2612,8 @@ export function createApiProxy(ctx: Context, defaults: ApiProxyDefaults): ApiPro
|
|||||||
agent.session.append('agent-preset/selected', { agentPreset: preset.id })
|
agent.session.append('agent-preset/selected', { agentPreset: preset.id })
|
||||||
return ok(request, { agentPreset: preset.id })
|
return ok(request, { agentPreset: preset.id })
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
if (error instanceof UnknownPresetError) {
|
const refused = presetFailure(request, error)
|
||||||
return err(request, {
|
if (refused !== undefined) return refused
|
||||||
code: 'agent-preset-not-found',
|
|
||||||
message: error.message,
|
|
||||||
details: { agentPreset: error.presetId, available: [...error.available] },
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if (error instanceof PresetMountError) {
|
|
||||||
return err(request, {
|
|
||||||
code: 'agent-preset-invalid',
|
|
||||||
message: error.message,
|
|
||||||
details: { agentPreset: error.presetId, reason: error.reason },
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return err(request, {
|
return err(request, {
|
||||||
code: 'internal',
|
code: 'internal',
|
||||||
message: `failed to select agent preset "${agentPreset}": ${String(error)}`,
|
message: `failed to select agent preset "${agentPreset}": ${String(error)}`,
|
||||||
|
|||||||
Reference in New Issue
Block a user