uptime.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import * as Sentry from '@sentry/react';
  2. import {
  3. addErrorMessage,
  4. addLoadingMessage,
  5. clearIndicators,
  6. } from 'sentry/actionCreators/indicator';
  7. import type {Client} from 'sentry/api';
  8. import {t} from 'sentry/locale';
  9. import type RequestError from 'sentry/utils/requestError/requestError';
  10. import type {UptimeRule} from 'sentry/views/alerts/rules/uptime/types';
  11. export async function updateUptimeRule(
  12. api: Client,
  13. orgId: string,
  14. uptimeMonitor: UptimeRule,
  15. data: Partial<UptimeRule>
  16. ): Promise<UptimeRule | null> {
  17. addLoadingMessage();
  18. try {
  19. const resp = await api.requestPromise(
  20. `/projects/${orgId}/${uptimeMonitor.projectSlug}/uptime/${uptimeMonitor.id}/`,
  21. {method: 'PUT', data}
  22. );
  23. clearIndicators();
  24. return resp;
  25. } catch (err) {
  26. const respError: RequestError = err;
  27. const updateKeys = Object.keys(data);
  28. // If we are updating a single value in the monitor we can read the
  29. // validation error for that key, otherwise fallback to the default error
  30. const validationError =
  31. updateKeys.length === 1
  32. ? (respError.responseJSON?.[updateKeys[0]!] as any)?.[0]
  33. : undefined;
  34. Sentry.captureException(err);
  35. addErrorMessage(validationError ?? t('Unable to update uptime monitor.'));
  36. }
  37. return null;
  38. }