Topic: A rejected save briefly displays our success alert

foxgrid_dev pro asked 1 week ago


The save request returns an HTTP error when a project name is already taken. Our green success alert appears anyway, followed by the red validation message after we inspect the returned JSON.

The success call is in the first then handler after fetch. Does a rejected HTTP status reach that handler, and where should we decide which notification to show?


Igor Przybysz staff answered 6 days ago


fetch resolves with a Response for ordinary HTTP error statuses. A resolved promise alone does not mean the save succeeded. Check response.ok and the endpoint's success contract before displaying the success alert or updating the saved state.

Keep the success notification in the successful branch and handle validation failures in the error branch. Use textContent for returned error text rather than inserting server-provided strings as HTML. This separates the request outcome from the alert's visual lifecycle.

const response = await fetch('/api/projects', requestOptions);
if (!response.ok) {
  throw new Error(`Save failed with HTTP ${response.status}`);
}
const savedProject = await response.json();
// Show success only after the successful response has been handled.

https://mdbootstrap.com/docs/standard/components/alerts/


foxgrid_dev pro commented 6 days ago

I had assumed that fetch rejected for every unsuccessful status. Moving the success notification after the response.ok check fixed the double message. The duplicate-name response now shows only the validation alert, and a successful save still shows the green one.



Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue

  • ForumUser: Pro
  • Premium support: No
  • Technology: MDB Standard
  • MDB Version: MDB5 9.3.0
  • Device: Laptop
  • Browser: Edge
  • OS: Windows 11
  • Provided sample code: No
  • Provided link: No