0

I tried angular error manager, I created a posts.component.ts component, I created a posts.service.ts service, I retrieved the posts by api: https://jsonplaceholder.typicode.com/posts, 100 posts, now generate an error, by deleting a non-existent post post n123. when I click on button delete, it does not manage any error either alert or console. list posts

post.component.html

<ul class="list-group">
    <li class="list-group-item" *ngFor="let post of posts">
        <h4>{{ post.title }}</h4>
        <p>{{ post.body }}</p>
        <div align="right">
            <button class="btn btn-warning" (click)="editPost(post)">Edit</button>
            <button class="btn btn-danger" (click)="deletePost(post)">Delete</button> 
       </div>
    </li>
</ul> 

post.service.ts

deletePost(post:any){
     return this.http.delete(this.url + "/" + post.id)
   }

post.component.ts

deletePost(post:any){
    this.postService.deletePost(post)
    .subscribe((response:any)=>{
          let index = this.posts.indexOf(post);
          index=123;
          this.posts.splice(index,1);
        },(error:Response) => {
          if(error.status === 404){
            alert("ce post deja supprimer")
          }else{
            alert("error server");
            console.log(error);
          }
        })
  };
2
  • What is the response of the API? Commented Jan 28, 2022 at 17:17
  • @Raffael thx for answer, response is list of post see picture i edit my code Commented Jan 28, 2022 at 17:19

2 Answers 2

1

This has nothing to do with your code. The API responds with a HTTP Status Code of 200, even when trying to delete a non existent post. This basically means, that even though nothing can be deleted because it doesn't exist https://jsonplaceholder.typicode.com/ responds that the operation went smoothly.

You can test this using a client of your choice, such as postman. I have saved the request here: https://documenter.getpostman.com/view/11980158/UVeCPTWx You can execute it by clicking on the top right "Run in Postman" and see that it returns with 200.

Sign up to request clarification or add additional context in comments.

Comments

0
this.Service.functionName(form)
.subscribe((response:any)=>{
      let index = this.posts.indexOf(post);
      index=123;
      this.posts.splice(index,1);
    },(error:Response) => {
      if(error.status === 404){
        alert("opps! Got error")
      }else{
        alert("error server");
        console.log(error);
      }
    })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.