How to catch request errors with httpResource in Angular v19

Discussão em 'Angular' iniciado por Jeanluca Scaljeri, Maio 11, 2025.

  1. I'm trying to use the new Angular httpResource. So I created a service as follows

    @Injectable({ providedIn: 'root' })
    export class MyService {
    resourceConfig = signal<Partial<ResourceConfig>>({})
    response = httpResource(() => ({
    url: this.resourceConfig().path,
    method: this.resourceConfig().method,
    body: this.resourceConfig.payload
    };
    })

    connect(payload: unknown) {
    this.resourceConfig.set({
    path: '/connect',
    method: 'POST',
    payload
    })
    }
    }


    This code works, the call to the connect endpoint is executed, but then I don't get anything back. For testing I've checked all the response signals in the component that calls connect:

    export class MyConnectComponent {
    private service = inject(MyService)

    connectResponseFn = effect(() => {
    const status = this.service.response.status(). // -> 2
    const error = this.service.response.error(). // -> undefined
    const resp = this.service.response.value(). // -> undefined
    const code = this.service.response.statusCode() // -> undefined
    })


    ngOnInit() {
    this.service.connect({test: true})
    }
    }


    How can I get responses in this situation?

    Continue reading...

Compartilhe esta Página