-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
fix(project-install): Render request errors at the bottom if any & disable project if rule is being created #92022
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
044009f
10e3afc
527c626
8d52a76
efa3dbf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {Project} from 'sentry/types/project'; | ||
import {useMutation} from 'sentry/utils/queryClient'; | ||
import RequestError from 'sentry/utils/requestError/requestError'; | ||
import useApi from 'sentry/utils/useApi'; | ||
import useOrganization from 'sentry/utils/useOrganization'; | ||
import {RequestDataFragment} from 'sentry/views/projectInstall/issueAlertOptions'; | ||
|
||
interface Variables | ||
extends Partial< | ||
Pick< | ||
RequestDataFragment, | ||
'conditions' | 'actions' | 'actionMatch' | 'frequency' | 'name' | ||
> | ||
> { | ||
projectSlug: string; | ||
} | ||
|
||
export function useCreateProjectRules() { | ||
const api = useApi(); | ||
const organization = useOrganization(); | ||
|
||
return useMutation<Project, RequestError, Variables>({ | ||
mutationFn: ({projectSlug, name, conditions, actions, actionMatch, frequency}) => { | ||
return api.requestPromise(`/projects/${organization.slug}/${projectSlug}/rules/`, { | ||
method: 'POST', | ||
data: { | ||
name, | ||
conditions, | ||
actions, | ||
actionMatch, | ||
frequency, | ||
}, | ||
}); | ||
}, | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import {PlatformIcon} from 'platformicons'; | |
|
||
import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator'; | ||
import {openModal} from 'sentry/actionCreators/modal'; | ||
import {removeProject} from 'sentry/actionCreators/projects'; | ||
import Access from 'sentry/components/acl/access'; | ||
import {Alert} from 'sentry/components/core/alert'; | ||
import {Button} from 'sentry/components/core/button'; | ||
|
@@ -18,6 +19,7 @@ import List from 'sentry/components/list'; | |
import ListItem from 'sentry/components/list/listItem'; | ||
import {SupportedLanguages} from 'sentry/components/onboarding/frameworkSuggestionModal'; | ||
import {useCreateProject} from 'sentry/components/onboarding/useCreateProject'; | ||
import {useCreateProjectRules} from 'sentry/components/onboarding/useCreateProjectRules'; | ||
import type {Platform} from 'sentry/components/platformPicker'; | ||
import PlatformPicker from 'sentry/components/platformPicker'; | ||
import TeamSelector from 'sentry/components/teamSelector'; | ||
|
@@ -26,6 +28,7 @@ import {space} from 'sentry/styles/space'; | |
import type {OnboardingSelectedSDK} from 'sentry/types/onboarding'; | ||
import type {Team} from 'sentry/types/organization'; | ||
import type {Project} from 'sentry/types/project'; | ||
import {defined} from 'sentry/utils'; | ||
import {trackAnalytics} from 'sentry/utils/analytics'; | ||
import {decodeScalar} from 'sentry/utils/queryString'; | ||
import useRouteAnalyticsEventNames from 'sentry/utils/routeAnalytics/useRouteAnalyticsEventNames'; | ||
|
@@ -133,12 +136,13 @@ const keyToErrorText: Record<string, string> = { | |
export function CreateProject() { | ||
const api = useApi(); | ||
const navigate = useNavigate(); | ||
const [errors, setErrors] = useState(false); | ||
const [errors, setErrors] = useState(); | ||
const organization = useOrganization(); | ||
const location = useLocation(); | ||
const {createNotificationAction, notificationProps} = useCreateNotificationAction(); | ||
const canUserCreateProject = useCanCreateProject(); | ||
const createProject = useCreateProject(); | ||
const createProjectRules = useCreateProjectRules(); | ||
const {teams} = useTeams(); | ||
const accessTeams = teams.filter((team: Team) => team.access.includes('team:admin')); | ||
const referrer = decodeScalar(location.query.referrer); | ||
|
@@ -153,24 +157,19 @@ export function CreateProject() { | |
project, | ||
alertRuleConfig, | ||
}: {project: Project} & Pick<FormData, 'alertRuleConfig'>) => { | ||
const ruleIds = []; | ||
const ruleIds: Array<string | undefined> = []; | ||
|
||
if (alertRuleConfig?.shouldCreateCustomRule) { | ||
const ruleData = await api.requestPromise( | ||
`/projects/${organization.slug}/${project.slug}/rules/`, | ||
{ | ||
method: 'POST', | ||
data: { | ||
name: project.name, | ||
conditions: alertRuleConfig?.conditions, | ||
actions: alertRuleConfig?.actions, | ||
actionMatch: alertRuleConfig?.actionMatch, | ||
frequency: alertRuleConfig?.frequency, | ||
}, | ||
} | ||
); | ||
const customRule = await createProjectRules.mutateAsync({ | ||
projectSlug: project.slug, | ||
name: project.name, | ||
actions: alertRuleConfig?.actions, | ||
conditions: alertRuleConfig?.conditions, | ||
actionMatch: alertRuleConfig?.actionMatch, | ||
frequency: alertRuleConfig?.frequency, | ||
}); | ||
|
||
ruleIds.push(ruleData.id); | ||
ruleIds.push(customRule.id); | ||
} | ||
|
||
const notificationRule = await createNotificationAction({ | ||
|
@@ -182,13 +181,11 @@ export function CreateProject() { | |
frequency: alertRuleConfig?.frequency, | ||
}); | ||
|
||
if (notificationRule) { | ||
ruleIds.push(notificationRule.id); | ||
} | ||
ruleIds.push(notificationRule?.id); | ||
|
||
return ruleIds; | ||
return ruleIds.filter(defined); | ||
}, | ||
[organization, api, createNotificationAction] | ||
[createNotificationAction, createProjectRules] | ||
); | ||
|
||
const autoFill = useMemo(() => { | ||
|
@@ -238,7 +235,10 @@ export function CreateProject() { | |
].filter(value => value).length; | ||
|
||
const canSubmitForm = | ||
!createProject.isPending && canUserCreateProject && formErrorCount === 0; | ||
!createProjectRules.isPending && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When a rule is being created, the primary button should be disabled as well because the request may fail |
||
!createProject.isPending && | ||
canUserCreateProject && | ||
formErrorCount === 0; | ||
|
||
const submitTooltipText = getSubmitTooltipText({ | ||
...missingValues, | ||
|
@@ -283,6 +283,8 @@ export function CreateProject() { | |
return; | ||
} | ||
|
||
let projectToRollback: Project | undefined; | ||
|
||
try { | ||
const project = await createProject.mutateAsync({ | ||
name: projectName, | ||
|
@@ -291,7 +293,9 @@ export function CreateProject() { | |
firstTeamSlug: team, | ||
}); | ||
|
||
const ruleIds = await createRules({project, alertRuleConfig}); | ||
projectToRollback = project; | ||
|
||
const ruleIds = await createRules({alertRuleConfig, project}); | ||
|
||
trackAnalytics('project_creation_page.created', { | ||
organization, | ||
|
@@ -339,9 +343,6 @@ export function CreateProject() { | |
) | ||
); | ||
} catch (error) { | ||
setErrors(!!error.responseJSON); | ||
addErrorMessage(t('Failed to create project %s', `${projectName}`)); | ||
|
||
// Only log this if the error is something other than: | ||
// * The user not having access to create a project, or, | ||
// * A project with that slug already exists | ||
|
@@ -351,9 +352,29 @@ export function CreateProject() { | |
Sentry.captureMessage('Project creation failed'); | ||
}); | ||
} | ||
setErrors(error.responseJSON); | ||
addErrorMessage(t('Failed to create project %s', `${projectName}`)); | ||
Comment on lines
+355
to
+356
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error response format is inconsistent. Sometimes it's a flat object with arrays, like: {
"actions": [
"Slack: The resource \"error\" does not exist or has not been granted access in the Sentry Slack workspace."
]
} Other times, it follows a different structure. For now, I’ve kept the existing logic mostly as-is and focused on fixing the immediate issue. We can revisit this later and improve error handling more cleanly. |
||
|
||
if (projectToRollback) { | ||
try { | ||
// Rolling back the project also deletes its associated alert rules | ||
// due to the cascading delete constraint. | ||
await removeProject({ | ||
api, | ||
orgSlug: organization.slug, | ||
projectSlug: projectToRollback.slug, | ||
origin: 'getting_started', | ||
}); | ||
} catch (err) { | ||
Sentry.withScope(scope => { | ||
scope.setExtra('error', err); | ||
Sentry.captureMessage('Failed to rollback project'); | ||
}); | ||
} | ||
} | ||
} | ||
}, | ||
[createRules, organization, createProject, setCreatedProject, navigate] | ||
[organization, createProject, setCreatedProject, navigate, api, createRules] | ||
); | ||
|
||
const handleProjectCreation = useCallback( | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the createPlatform function is getting complex - happy to clean it up in a follow-up