Skip to content

Commit

Permalink
feat(http): Throw errors for schema mismatch (#21395)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Apr 11, 2023
1 parent 2dae252 commit e1d5012
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
18 changes: 8 additions & 10 deletions lib/util/http/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ describe('util/http/index', () => {
expect(logger.logger.once.info).not.toHaveBeenCalled();
});

it('returns original body if schema does not match', async () => {
it('throws on schema mismatch', async () => {
httpMock
.scope(baseUrl, {
reqheaders: {
Expand All @@ -359,10 +359,9 @@ describe('util/http/index', () => {
.get('/')
.reply(200, JSON.stringify({ foo: 'bar' }));

const { body } = await http.getJson('http://renovate.com', SomeSchema);

expect(body).toEqual({ foo: 'bar' });
expect(logger.logger.once.info).toHaveBeenCalled();
await expect(
http.getJson('http://renovate.com', SomeSchema)
).rejects.toThrow(z.ZodError);
});
});

Expand All @@ -382,16 +381,15 @@ describe('util/http/index', () => {
expect(logger.logger.once.info).not.toHaveBeenCalled();
});

it('returns original body if schema does not match', async () => {
it('throws on schema mismatch', async () => {
httpMock
.scope(baseUrl)
.post('/')
.reply(200, JSON.stringify({ foo: 'bar' }));

const { body } = await http.postJson('http://renovate.com', SomeSchema);

expect(body).toEqual({ foo: 'bar' });
expect(logger.logger.once.info).toHaveBeenCalled();
await expect(
http.postJson('http://renovate.com', SomeSchema)
).rejects.toThrow(z.ZodError);
});
});
});
Expand Down
11 changes: 1 addition & 10 deletions lib/util/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,19 +259,10 @@ export class Http<Opts extends HttpOptions = HttpOptions> {
const res = await this.request<ResT>(url, opts);

if (!schema) {
return { ...res, body: res.body };
}

const parsed = await schema.safeParseAsync(res.body);
if (!parsed.success) {
logger.once.info(
{ err: parsed.error },
`Response does not match schema: please report this to https://github.com/renovatebot/renovate/pull/21338`
);
return res;
}

res.body = parsed.data;
res.body = await schema.parseAsync(res.body);
return res;
}

Expand Down

0 comments on commit e1d5012

Please sign in to comment.