Skip to content

Commit

Permalink
Refactors the maybe throw function to be general and reusable, with a…
Browse files Browse the repository at this point in the history
… new wrapper function that does just that
  • Loading branch information
dakota002 committed Mar 7, 2025
1 parent ab925fb commit 5155121
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 24 deletions.
21 changes: 6 additions & 15 deletions app/lib/cognito.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
paginateListUsersInGroup,
} from '@aws-sdk/client-cognito-identity-provider'

import { maybeThrow } from './utils'
import type { User } from '~/routes/_auth/user.server'

export const gcnGroupPrefix = 'gcn.nasa.gov/'
Expand Down Expand Up @@ -142,24 +143,14 @@ export async function listUsersInGroup(GroupName: string) {
return users
}

export function maybeThrow(e: any, warning: string) {
const errorsAllowedInDev = [
export function maybeThrowCognito(e: any, warning: string) {
const formattedWarning = `Cognito threw ${(e as CognitoIdentityProviderServiceException).name}. This would be an error in production. Since we are in ${process.env.NODE_ENV}, ${warning}.`

Check warning on line 147 in app/lib/cognito.server.ts

View check run for this annotation

Codecov / codecov/patch

app/lib/cognito.server.ts#L147

Added line #L147 was not covered by tests

maybeThrow<CognitoIdentityProviderServiceException>(e, formattedWarning, [

Check warning on line 149 in app/lib/cognito.server.ts

View check run for this annotation

Codecov / codecov/patch

app/lib/cognito.server.ts#L149

Added line #L149 was not covered by tests
'ExpiredTokenException',
'NotAuthorizedException',
'UnrecognizedClientException',
]
const { name } = e as CognitoIdentityProviderServiceException

if (
!errorsAllowedInDev.includes(name) ||
process.env.NODE_ENV === 'production'
) {
throw e
} else {
console.warn(
`Cognito threw ${name}. This would be an error in production. Since we are in ${process.env.NODE_ENV}, ${warning}.`
)
}
])
}

export async function createGroup(GroupName: string, Description: string) {
Expand Down
21 changes: 21 additions & 0 deletions app/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,24 @@ export function throwForStatus(response: Response) {
throw new Error('Request failed', { cause: response })
}
}

interface ErrorType {
name: string
}

export function maybeThrow<Type extends ErrorType>(
e: Type,
warning: string,
errorsAllowedInDev: string[]
) {
const { name } = e as Type

Check warning on line 90 in app/lib/utils.ts

View check run for this annotation

Codecov / codecov/patch

app/lib/utils.ts#L90

Added line #L90 was not covered by tests

if (
!errorsAllowedInDev.includes(name) ||
process.env.NODE_ENV === 'production'
) {
throw e

Check warning on line 96 in app/lib/utils.ts

View check run for this annotation

Codecov / codecov/patch

app/lib/utils.ts#L96

Added line #L96 was not covered by tests
} else {
console.warn(warning)

Check warning on line 98 in app/lib/utils.ts

View check run for this annotation

Codecov / codecov/patch

app/lib/utils.ts#L98

Added line #L98 was not covered by tests
}
}
4 changes: 2 additions & 2 deletions app/routes/user._index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { getUser, updateSession } from './_auth/user.server'
import { formatAuthor } from './circulars/circulars.lib'
import Hint from '~/components/Hint'
import Spinner from '~/components/Spinner'
import { cognito, maybeThrow } from '~/lib/cognito.server'
import { cognito, maybeThrowCognito } from '~/lib/cognito.server'

Check warning on line 26 in app/routes/user._index.tsx

View check run for this annotation

Codecov / codecov/patch

app/routes/user._index.tsx#L26

Added line #L26 was not covered by tests
import { getFormDataString } from '~/lib/utils'
import type { BreadcrumbHandle } from '~/root/Title'

Expand Down Expand Up @@ -68,7 +68,7 @@ export async function action({ request }: ActionFunctionArgs) {
try {
await cognito.send(command)
} catch (e) {
maybeThrow(e, 'not saving name and affiliation permanently')
maybeThrowCognito(e, 'not saving name and affiliation permanently')

Check warning on line 71 in app/routes/user._index.tsx

View check run for this annotation

Codecov / codecov/patch

app/routes/user._index.tsx#L71

Added line #L71 was not covered by tests
}

user.name = name
Expand Down
10 changes: 5 additions & 5 deletions app/routes/user.credentials/client_credentials.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from '@aws-sdk/client-cognito-identity-provider'
import { generators } from 'openid-client'

import { cognito, maybeThrow } from '~/lib/cognito.server'
import { cognito, maybeThrowCognito } from '~/lib/cognito.server'

Check warning on line 17 in app/routes/user.credentials/client_credentials.server.ts

View check run for this annotation

Codecov / codecov/patch

app/routes/user.credentials/client_credentials.server.ts#L17

Added line #L17 was not covered by tests
import { getUser } from '~/routes/_auth/user.server'

export interface RedactedClientCredential {
Expand Down Expand Up @@ -155,7 +155,7 @@ export class ClientCredentialVendingMachine {
try {
response = await cognito.send(command)
} catch (e) {
maybeThrow(e, 'creating fake client credentials')
maybeThrowCognito(e, 'creating fake client credentials')

Check warning on line 158 in app/routes/user.credentials/client_credentials.server.ts

View check run for this annotation

Codecov / codecov/patch

app/routes/user.credentials/client_credentials.server.ts#L158

Added line #L158 was not covered by tests
const client_id = generators.random(26)
const client_secret = generators.random(51)
return { client_id, client_secret }
Expand All @@ -178,7 +178,7 @@ export class ClientCredentialVendingMachine {
try {
response = await cognito.send(command)
} catch (e) {
maybeThrow(e, 'creating fake client secret')
maybeThrowCognito(e, 'creating fake client secret')

Check warning on line 181 in app/routes/user.credentials/client_credentials.server.ts

View check run for this annotation

Codecov / codecov/patch

app/routes/user.credentials/client_credentials.server.ts#L181

Added line #L181 was not covered by tests
const client_secret = generators.random(51)
return client_secret
}
Expand All @@ -197,7 +197,7 @@ export class ClientCredentialVendingMachine {
try {
await cognito.send(command)
} catch (e) {
maybeThrow(e, 'deleting fake client credentials')
maybeThrowCognito(e, 'deleting fake client credentials')

Check warning on line 200 in app/routes/user.credentials/client_credentials.server.ts

View check run for this annotation

Codecov / codecov/patch

app/routes/user.credentials/client_credentials.server.ts#L200

Added line #L200 was not covered by tests
}
}

Expand All @@ -215,7 +215,7 @@ export class ClientCredentialVendingMachine {
try {
response = await cognito.send(command)
} catch (e) {
maybeThrow(e, 'not getting group descriptions')
maybeThrowCognito(e, 'not getting group descriptions')

Check warning on line 218 in app/routes/user.credentials/client_credentials.server.ts

View check run for this annotation

Codecov / codecov/patch

app/routes/user.credentials/client_credentials.server.ts#L218

Added line #L218 was not covered by tests
}

const groupsMap: { [key: string]: string | undefined } = Object.fromEntries(
Expand Down
4 changes: 2 additions & 2 deletions app/routes/user.endorsements/endorsements.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
extractAttributeRequired,
getCognitoUserFromSub,
listUsersInGroup,
maybeThrow,
maybeThrowCognito,
} from '~/lib/cognito.server'
import { sendEmail } from '~/lib/email.server'
import { origin } from '~/lib/env.server'
Expand Down Expand Up @@ -373,7 +373,7 @@ async function getUsersInGroup(): Promise<EndorsementUser[]> {
try {
users = await listUsersInGroup(submitterGroup)
} catch (error) {
maybeThrow(error, 'returning fake users')
maybeThrowCognito(error, 'returning fake users')

Check warning on line 376 in app/routes/user.endorsements/endorsements.server.ts

View check run for this annotation

Codecov / codecov/patch

app/routes/user.endorsements/endorsements.server.ts#L376

Added line #L376 was not covered by tests
return [
{
sub: crypto.randomUUID(),
Expand Down

0 comments on commit 5155121

Please sign in to comment.