echo75 / my-movie-db

MIT License
1 stars 0 forks source link

#API.Design Refactoring try catch into an interceptor #11

Open Dr4gon opened 1 year ago

Dr4gon commented 1 year ago

@echo75 Here's a good article on the topic: How to implement Interceptors in JS

You currently have boiler plate code spread across some services= code that is necessary and also repetitive. E.g. getters/setters. The idea with interceptors is quite simple. You intercept in this case each REST call of users.js and on error send a proper error message and a proper HTTP code.

WatchList.vue

async fetchMovies() {
      try {
        const response = await axios.get(`/users/${this.user._id}/watchlist`)
        this.movies = response.data // Assuming the API response contains an array of movies
      } catch (error) {
        console.error('Error fetching movies:', error)
      }
    },
    async deleteMovie(id) {
      try {
        const response = await axios.delete(`/users/${this.user._id}/watchlist/${id}`)
        console.log(response)
      } catch (error) {
        console.error('Error deleting movie:', error)
      }
    },
    async moveMovie(id) {
      try {
        const response = await axios.post(`/users/${this.user._id}/watchedlist/${id}`)
        console.log(response)
      } catch (error) {
        console.error('Error marking movie as watched:', error)
      }
    },