[BUGFIX] Issue 7916 - handle invalid response if payload is null
Description
This fixes issue #7916. Handle an Invalid response though if the payload is null.
Done the change and linked this to a sample Ember app and tested by using a mock API returning status 422 and response as null
HI @runspired , I tried to add test for this change in tests/main/tests/integration/adapter/handle-response-test.js as below:
But this didn't get failed when I removed the condition that I added in packages/adapter/addon/rest.ts. I was expecting a typeError when payload was passed null. I used this command to test pnpm --filter main-test-app run test.
test('handleResponse is called with correct parameters on null response with 422 status', async function (assert) {
let handleResponseCalled = 0;
this.server.get('/people', function () {
return ['422', { 'Content-Type': 'application/json' }, null];
});
class TestAdapter extends JSONAPIAdapter {
handleResponse(status, headers, payload, requestData) {
console.log(payload, 'payload');
handleResponseCalled++;
return super.handleResponse(status, headers, payload, requestData);
}
}
this.owner.register('adapter:application', TestAdapter);
try {
await this.store.findAll('person');
assert.ok(false, 'promise should reject');
} catch {
assert.ok(true, 'promise rejected');
}
assert.strictEqual(handleResponseCalled, 1, 'handle response is called');
});
@mani-rsg let's still add that test but we will need to tweak it to ensure the error we are catching is the appropriate error. Since in both cases we throw, we just throw something unexpected in the payload=null case.
@mani-rsg were you still interested in this?