Angular 9 : rxjs 6 : how to return a manual Observable of Error

426 Views Asked by At

In my Angular app , and to test some features in HorLigne mode , i need to mock http usefull responses :

  • the 200 OK return containing some data
  • the 404 KO return with no data

Usually i tried this :

@Injectable()
export class VerifService {
  mockedResult = false;
  verifData(mail, uuid) {
    if (this.mockedResult) {
      return ObservaleOf({ status: "OK" }).pipe(delay(3000));
    } else {
      return throwError({ status: 500, message: "not found" }).pipe(
        delay(3000)
      );
    }
  }
}

After that within my component i m doing this :

this.verifService.verifData(mail, uuid).subscribe(data=>{
  console.log('THIS WORKS')
}, err => {
  console.log('THIS FAILS')
}

For the case mockedResult equals true , the successful 200 OK return is working

But for the 404 error return , it's not working

Is there any other good way to mock the 404 error observable return ?

Suggestions ?

1

There are 1 best solutions below

0
On

Not sure where is not working. If you need to pause for some time:

interval(1000).pipe(
  switchMap(
    () => throwError(new Error())
  )
)

Pipe will not work as when throwError the observable already been stopped.