2

I have an application running on angular and I already have an http intercept setup. My issue is that my api returns some 404 errors that I would want to redirect to a 404 page and some that I wouldn't. For example when navigating to a new page, if the content of that page returns a 404 I want to direct to a 404 page instead of loading an empty template. However on a page where a user is checking out (paying for a purchased item) I check to see if they have a credit card token stored on file. If they do we can offer them the choice to use it. If they don't have one on file the api returns a 404 and we ask them to enter one.

My issue is that because of these two cases, it's not as simple at calling $state.go('404') anytime a 404 is thrown. I'm weighing a few options. One, have the api return a special message if it should redirect to a 404. This seems less than ideal, not really the responsibility of the api and we have multiple clients on a shared api. I could try to detect the current state/page in the http intercept and create a list of states that should redirect. I could $rootScope.$broadcast('no-template-data') or something similar from each controller that needs this and redirect from an global app.run function.

Has anyone else faced this challenge with single page applications?

Constellates
  • 2,083
  • 3
  • 19
  • 29
  • Returning 404 on app logic errors is wrong. There's no other way than treating a 404 differently based on the action that triggered it (except fixing the API). Ideally you'd treat it application-wide. – Sergiu Paraschiv Jun 19 '14 at 15:13
  • That makes sense. And I can definitely get that change made. – Constellates Jun 19 '14 at 15:46
  • Here's how you implement an application-wide 404 interceptor: http://stackoverflow.com/questions/13603078/angular-http-404-error-handling Next you need to change the API and return some other code for application specific logic (the 4xx class is suitable for this, just pick codes that are not already designated some meaning by the protocol - 400-417 for HTTP/1.1 I think). – Sergiu Paraschiv Jun 19 '14 at 15:57
  • Check https://stackoverflow.com/questions/37334220/how-do-i-return-a-http-404-status-code-from-a-spa/48510017#48510017 – Mauro Jan 30 '18 at 14:54

0 Answers0