Skip to content

Commit

Permalink
Avoid unsuitable type parameter inference via unrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
weltenwort committed Feb 5, 2021
1 parent feb6854 commit 8cc9eed
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions x-pack/plugins/infra/common/http_api/shared/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,35 @@

import * as rt from 'io-ts';

const createErrorRuntimeType = <Attributes extends rt.Mixed = rt.UndefinedType>(
statusCode: number,
errorCode: string,
attributes?: Attributes
) =>
export const badRequestErrorRT = rt.intersection([
rt.type({
statusCode: rt.literal(statusCode),
error: rt.literal(errorCode),
statusCode: rt.literal(400),
error: rt.literal('Bad Request'),
message: rt.string,
...(!!attributes ? { attributes } : {}),
});
}),
rt.partial({
attributes: rt.unknown,
}),
]);

export const badRequestErrorRT = createErrorRuntimeType(400, 'Bad Request');
export const forbiddenErrorRT = createErrorRuntimeType(403, 'Forbidden');
export const conflictErrorRT = createErrorRuntimeType(409, 'Conflict');
export const forbiddenErrorRT = rt.intersection([
rt.type({
statusCode: rt.literal(403),
error: rt.literal('Forbidden'),
message: rt.string,
}),
rt.partial({
attributes: rt.unknown,
}),
]);

export const conflictErrorRT = rt.intersection([
rt.type({
statusCode: rt.literal(409),
error: rt.literal('Conflict'),
message: rt.string,
}),
rt.partial({
attributes: rt.unknown,
}),
]);

0 comments on commit 8cc9eed

Please sign in to comment.