Im trying to code a “Template”-Service for my TypeORM tables, but i cant manage the errors like i want.
I tried something like this:
My TableService:
import { DeleteResult, FindOptionsWhere, Repository } from "typeorm";
import { Observable, catchError, from, map, throwError } from 'rxjs';
export class TableService<E, PKs> {
constructor(
private repository: Repository<E>
){}
delete(primKeys: PKs): Observable<DeleteResult> {
if (Object.keys(primKeys).length === 0) {
return throwError(() => new Error('No primKeys set.'));
} else {
try{
return from(this.repository.delete(primKeys as FindOptionsWhere<E>)).pipe(
map((delRes: DeleteResult) => { return delRes; }),
catchError((err) => {
console.log("In catchError");
return throwError(() => new Error("err.message"));
})
);
} catch(err){
console.log("outer chatch");
}
}
}
}
The requests are working, if the Query-Params are correct.
But the error handling is not working as i expected.
The error is not caught in catchError, but rather in the outer catch.
I dont understand why…
Lambdanos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.