Skip to content

Commit

Permalink
properly handle errors when posting CSR's
Browse files Browse the repository at this point in the history
  • Loading branch information
kayra1 committed Aug 13, 2024
1 parent 62a3e59 commit 57e6c3a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
23 changes: 19 additions & 4 deletions ui/src/app/certificate_requests/asideForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ import { AsideContext } from "../aside";
export default function CertificateRequestsAsidePanel(): JSX.Element {
const asideContext = useContext(AsideContext)
const [cookies, setCookie, removeCookie] = useCookies(['user_token']);
const [errorText, setErrorText] = useState<string>("")
const queryClient = useQueryClient()
const mutation = useMutation(postCSR, {
onSuccess: () => {
setErrorText("")
asideContext.setIsOpen(false)
queryClient.invalidateQueries('csrs')
},
onError: (e: Error) => {
setErrorText(e.message)
}
})
const [CSRPEMString, setCSRPEMString] = useState<string>("")
const handleTextChange = (event: ChangeEvent<HTMLTextAreaElement>) => {
Expand Down Expand Up @@ -53,26 +58,36 @@ export default function CertificateRequestsAsidePanel(): JSX.Element {
<input type="file" name="upload" accept=".pem,.csr" onChange={handleFileChange}></input>
</div>
<div className="p-form__group row">
<SubmitCSR csrText={CSRPEMString} onClickFunc={() => mutation.mutate({ authToken: cookies.user_token, csr: CSRPEMString })} />
<SubmitCSR csrText={CSRPEMString} errorText={errorText} onClickFunc={() => mutation.mutate({ authToken: cookies.user_token, csr: CSRPEMString })} />
</div>
</form>
</div>
</div >
)
}

function SubmitCSR({ csrText, onClickFunc }: { csrText: string, onClickFunc: any }) {
function SubmitCSR({ csrText, errorText, onClickFunc }: { csrText: string, errorText: string, onClickFunc: any }) {
let csrIsValid = false
try {
extractCSR(csrText.trim())
csrIsValid = true
}
catch { }

const validationComponent = csrText == "" ? <></> : csrIsValid ? <div><i className="p-icon--success"></i>Valid CSR</div> : <div><i className="p-icon--error"></i>Invalid CSR</div>
const buttonComponent = csrIsValid ? <button className="p-button--positive u-float-right" name="submit" onClick={onClickFunc} >Submit</button> : <button className="p-button--positive u-float-right" name="submit" disabled={true} onClick={onClickFunc} >Submit</button>
const buttonComponent = csrIsValid ? (
<button className="p-button--positive u-float-right" name="submit" onClick={(e) => { e.preventDefault(); onClickFunc() }} > Submit</button >
) : (
<button className="p-button--positive u-float-right" name="submit" disabled={true} onClick={(e) => { e.preventDefault(); onClickFunc() }} >Submit</button>)
return (
<>
{errorText != "" &&
<div className="p-notification--negative">
<div className="p-notification__content">
<h5 className="p-notification__title">Error</h5>
<p className="p-notification__message">{errorText.split("error: ")}</p>
</div>
</div>
}
{validationComponent}
{buttonComponent}
</>
Expand Down
3 changes: 2 additions & 1 deletion ui/src/app/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ export async function postCSR(params: { authToken: string, csr: string }) {
},
body: params.csr.trim()
})
const responseText = await response.text()
if (!response.ok) {
throw new Error(`${response.status}: ${HTTPStatus(response.status)}`)
throw new Error(`${response.status}: ${HTTPStatus(response.status)}. ${responseText}`)
}
return response.json()
}
Expand Down

0 comments on commit 57e6c3a

Please sign in to comment.