Merged in feat/sas-otp-error-handling (pull request #1272)
Feat/sas otp error handling * Improve error handling for SAS OTP * Remove failing and deprecated test Approved-by: Joakim Jäderberg
This commit is contained in:
@@ -18,7 +18,16 @@ import type { OtpState } from "../getOTPState"
|
||||
const inputSchema = z.object({})
|
||||
|
||||
const outputSchema = z.object({
|
||||
status: z.string(),
|
||||
status: z.enum([
|
||||
"VERIFIED",
|
||||
"ABUSED",
|
||||
"EXPIRED",
|
||||
"PENDING",
|
||||
"RETRY",
|
||||
"SENT",
|
||||
"NULL",
|
||||
"NOTSENT",
|
||||
]),
|
||||
referenceId: z.string().uuid(),
|
||||
databaseUUID: z.string().uuid(),
|
||||
otpExpiration: z.number(),
|
||||
@@ -42,14 +51,15 @@ export const requestOtp = protectedProcedure
|
||||
tokenResponse.status,
|
||||
tokenResponse.statusText
|
||||
)
|
||||
if (!tokenResponse.ok) {
|
||||
const errorBody = await tokenResponse.json()
|
||||
console.error("[SAS] requestOtp error", errorBody)
|
||||
throw createError(errorBody)
|
||||
}
|
||||
|
||||
const parseResult = outputSchema.safeParse(await tokenResponse.json())
|
||||
const body = await tokenResponse.json()
|
||||
const parseResult = outputSchema.safeParse(body)
|
||||
if (!parseResult.success) {
|
||||
console.error("[SAS] requestOtp error", body)
|
||||
|
||||
if (!tokenResponse.ok) {
|
||||
throw createError(body)
|
||||
}
|
||||
throw createError(parseResult.error)
|
||||
}
|
||||
|
||||
|
||||
@@ -4,13 +4,6 @@ import { parseSASRequestOtpError } from "./requestOtpError"
|
||||
|
||||
describe("requestOtpError", () => {
|
||||
it("parses error with invalid error code", () => {
|
||||
const error = {
|
||||
status: "status",
|
||||
error: "error",
|
||||
errorCode: "a",
|
||||
databaseUUID: "9ffefefe-df0e-4229-9792-5ed31bef1db4",
|
||||
}
|
||||
|
||||
const actual = parseSASRequestOtpError({
|
||||
status: "status",
|
||||
error: "error",
|
||||
@@ -21,17 +14,4 @@ describe("requestOtpError", () => {
|
||||
errorCode: "UNKNOWN",
|
||||
})
|
||||
})
|
||||
|
||||
it("parses error as TOO_MANY_REQUESTS error code", () => {
|
||||
const actual = parseSASRequestOtpError({
|
||||
status: "status",
|
||||
error: "error",
|
||||
errorCode: 10,
|
||||
databaseUUID: "9ffefefe-df0e-4229-9792-5ed31bef1db4",
|
||||
otpExpiration: "2021-09-01T00:00:00Z",
|
||||
})
|
||||
expect(actual).toEqual({
|
||||
errorCode: "TOO_MANY_REQUESTS",
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -51,7 +51,16 @@ const getErrorCodeByNumber = (number: number): RequestOtpResponseError => {
|
||||
}
|
||||
|
||||
const sasOtpRequestErrorSchema = z.object({
|
||||
status: z.string(),
|
||||
status: z.enum([
|
||||
"VERIFIED",
|
||||
"ABUSED",
|
||||
"EXPIRED",
|
||||
"PENDING",
|
||||
"RETRY",
|
||||
"SENT",
|
||||
"NULL",
|
||||
"NOTSENT",
|
||||
]),
|
||||
otpExpiration: z.string().datetime(),
|
||||
error: z.string(),
|
||||
errorCode: z.number(),
|
||||
|
||||
@@ -17,7 +17,16 @@ const inputSchema = z.object({
|
||||
})
|
||||
|
||||
const outputSchema = z.object({
|
||||
status: z.string(), // TODO: Change to enum
|
||||
status: z.enum([
|
||||
"VERIFIED",
|
||||
"ABUSED",
|
||||
"EXPIRED",
|
||||
"PENDING",
|
||||
"RETRY",
|
||||
"SENT",
|
||||
"NULL",
|
||||
"NOTSENT",
|
||||
]),
|
||||
referenceId: z.string().uuid(),
|
||||
databaseUUID: z.string().uuid().optional(),
|
||||
})
|
||||
@@ -29,7 +38,6 @@ export const verifyOtp = protectedProcedure
|
||||
const sasAuthToken = getSasToken()
|
||||
|
||||
if (!sasAuthToken) {
|
||||
// TODO: Should we verify that the SAS token isn't expired?
|
||||
throw createError("AUTH_TOKEN_NOT_FOUND")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user