Skip to content

Commit

Permalink
mapping the error instead of showing from the backend
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaElastic committed Feb 17, 2022
1 parent 1333a06 commit b6b06d0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const StyledEuiAccordion = styled(EuiAccordion)`
`;

interface Props {
updateAgentPolicy: (u: AgentPolicy | null, errorMessage?: string) => void;
updateAgentPolicy: (u: AgentPolicy | null, errorMessage?: JSX.Element) => void;
isFleetServerPolicy?: boolean;
agentPolicyName: string;
}
Expand Down Expand Up @@ -84,12 +84,24 @@ export const AgentPolicyCreateInlineForm: React.FunctionComponent<Props> = ({
updateAgentPolicy(resp.data.item);
}
} catch (e) {
updateAgentPolicy(null, e.message);
updateAgentPolicy(null, mapError(e));
} finally {
setIsLoading(false);
}
}, [newAgentPolicy, withSysMonitoring, updateAgentPolicy]);

function mapError(e: { statusCode: number }): JSX.Element | undefined {
switch (e.statusCode) {
case 409:
return (
<FormattedMessage
id="xpack.fleet.agentPolicyCreation.errorMessage"
defaultMessage="An agent policy already exists with this name."
/>
);
}
}

return (
<EuiForm>
<EuiText>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export enum CREATE_STATUS {

export interface AgentPolicyCreateState {
status: CREATE_STATUS;
errorMessage?: string;
errorMessage?: JSX.Element;
}

interface Props {
Expand Down Expand Up @@ -52,12 +52,7 @@ export const AgentPolicyCreatedCallOut: React.FunctionComponent<Props> = ({ crea
color="danger"
iconType="cross"
>
{createState.errorMessage ? (
<FormattedMessage
id="xpack.fleet.agentPolicyCreation.errorMessage"
defaultMessage={createState.errorMessage}
/>
) : null}
{createState.errorMessage ?? null}
</EuiCallOut>
)}
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const SelectCreateAgentPolicy: React.FC<Props> = ({
}, [agentPolicies, isFleetServerPolicy]);

const onAgentPolicyCreated = useCallback(
async (policy: AgentPolicy | null, errorMessage?: string) => {
async (policy: AgentPolicy | null, errorMessage?: JSX.Element) => {
if (!policy) {
setCreateState({ status: CREATE_STATUS.FAILED, errorMessage });
return;
Expand Down

0 comments on commit b6b06d0

Please sign in to comment.