Skip to content

Commit 34bbf64

Browse files
authored
mock API: invalidRequest helper (#3145)
1 parent 93ccb53 commit 34bbf64

3 files changed

Lines changed: 18 additions & 46 deletions

File tree

mock-api/msw/db.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { commaSeries } from '~/util/str'
2020
import type { Json } from '../json-type'
2121
import { projects, type DbProject } from '../project'
2222
import { defaultSilo, siloSettings } from '../silo'
23-
import { internalError } from './util'
23+
import { internalError, invalidRequest } from './util'
2424

2525
export const notFoundErr = (msg: string) => {
2626
const message = `not found: ${msg}`
@@ -52,7 +52,7 @@ function ensureNoParentSelectors(
5252
.map(([k]) => k)
5353
if (keysWithValues.length > 0) {
5454
const message = `when ${resourceLabel} is specified by ID, ${commaSeries(keysWithValues, 'and')} should not be specified`
55-
throw json({ error_code: 'InvalidRequest', message }, { status: 400 })
55+
throw invalidRequest(message)
5656
}
5757
}
5858

@@ -101,13 +101,7 @@ export const resolvePoolSelector = (
101101
!poolSelector.ip_version &&
102102
candidateLinks.length > 1
103103
) {
104-
throw json(
105-
{
106-
error_code: 'InvalidRequest',
107-
message: 'ip_version required when multiple default pools exist',
108-
},
109-
{ status: 400 }
110-
)
104+
throw invalidRequest('ip_version required when multiple default pools exist')
111105
}
112106

113107
const link = candidateLinks[0]

mock-api/msw/handlers.ts

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import {
5555
getBlockSize,
5656
handleMetrics,
5757
handleOxqlMetrics,
58+
invalidRequest,
5859
ipRangeLen,
5960
NotImplemented,
6061
paginated,
@@ -558,21 +559,13 @@ export const handlers = makeHandlers({
558559
// Based on Omicron validation in nexus/db-queries/src/db/datastore/external_ip.rs:544-661
559560
const ipVersion = pool.ip_version
560561
if (ipVersion === 'v4' && !hasIpv4Nic) {
561-
throw json(
562-
{
563-
error_code: 'InvalidRequest',
564-
message: `The ephemeral external IP is an IPv4 address, but the instance with ID ${body.name} does not have a primary network interface with a VPC-private IPv4 address. Add a VPC-private IPv4 address to the interface, or attach a different IP address`,
565-
},
566-
{ status: 400 }
562+
throw invalidRequest(
563+
`The ephemeral external IP is an IPv4 address, but the instance with ID ${body.name} does not have a primary network interface with a VPC-private IPv4 address. Add a VPC-private IPv4 address to the interface, or attach a different IP address`
567564
)
568565
}
569566
if (ipVersion === 'v6' && !hasIpv6Nic) {
570-
throw json(
571-
{
572-
error_code: 'InvalidRequest',
573-
message: `The ephemeral external IP is an IPv6 address, but the instance with ID ${body.name} does not have a primary network interface with a VPC-private IPv6 address. Add a VPC-private IPv6 address to the interface, or attach a different IP address`,
574-
},
575-
{ status: 400 }
567+
throw invalidRequest(
568+
`The ephemeral external IP is an IPv6 address, but the instance with ID ${body.name} does not have a primary network interface with a VPC-private IPv6 address. Add a VPC-private IPv6 address to the interface, or attach a different IP address`
576569
)
577570
}
578571
}
@@ -888,35 +881,21 @@ export const handlers = makeHandlers({
888881
const primaryNic = nics.find((n) => n.primary)
889882

890883
if (!primaryNic) {
891-
throw json(
892-
{
893-
error_code: 'InvalidRequest',
894-
message: `Instance ${instance.name} has no primary network interface`,
895-
},
896-
{ status: 400 }
897-
)
884+
throw invalidRequest(`Instance ${instance.name} has no primary network interface`)
898885
}
899886

900887
const ipVersion = pool.ip_version
901888
const stackType = primaryNic.ip_stack.type
902889

903890
if (ipVersion === 'v4' && stackType !== 'v4' && stackType !== 'dual_stack') {
904-
throw json(
905-
{
906-
error_code: 'InvalidRequest',
907-
message: `The ephemeral external IP is an IPv4 address, but the instance with ID ${instance.name} does not have a primary network interface with a VPC-private IPv4 address. Add a VPC-private IPv4 address to the interface, or attach a different IP address`,
908-
},
909-
{ status: 400 }
891+
throw invalidRequest(
892+
`The ephemeral external IP is an IPv4 address, but the instance with ID ${instance.name} does not have a primary network interface with a VPC-private IPv4 address. Add a VPC-private IPv4 address to the interface, or attach a different IP address`
910893
)
911894
}
912895

913896
if (ipVersion === 'v6' && stackType !== 'v6' && stackType !== 'dual_stack') {
914-
throw json(
915-
{
916-
error_code: 'InvalidRequest',
917-
message: `The ephemeral external IP is an IPv6 address, but the instance with ID ${instance.name} does not have a primary network interface with a VPC-private IPv6 address. Add a VPC-private IPv6 address to the interface, or attach a different IP address`,
918-
},
919-
{ status: 400 }
897+
throw invalidRequest(
898+
`The ephemeral external IP is an IPv6 address, but the instance with ID ${instance.name} does not have a primary network interface with a VPC-private IPv6 address. Add a VPC-private IPv6 address to the interface, or attach a different IP address`
920899
)
921900
}
922901

@@ -949,12 +928,8 @@ export const handlers = makeHandlers({
949928
)
950929

951930
if (attachedVersions.size > 1 && !ipVersion) {
952-
throw json(
953-
{
954-
error_code: 'InvalidRequest',
955-
message: `Instance ${instance.name} has both IPv4 and IPv6 ephemeral IPs; ipVersion is required to detach one`,
956-
},
957-
{ status: 400 }
931+
throw invalidRequest(
932+
`Instance ${instance.name} has both IPv4 and IPv6 ephemeral IPs; ipVersion is required to detach one`
958933
)
959934
}
960935

mock-api/msw/util.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ export const NotImplemented = () => {
110110
throw json({ error_code: 'NotImplemented' }, { status: 501 })
111111
}
112112

113+
export const invalidRequest = (message: string) =>
114+
json({ error_code: 'InvalidRequest', message }, { status: 400 })
115+
113116
export const internalError = (message: string) =>
114117
json({ error_code: 'InternalError', message }, { status: 500 })
115118

0 commit comments

Comments
 (0)