catch json parsing problem and report nicely
This commit is contained in:
parent
b34f3568e8
commit
7330f0daf9
@ -139,7 +139,22 @@ type ResponseOrError<T> =
|
|||||||
export async function readTalerErrorResponse(
|
export async function readTalerErrorResponse(
|
||||||
httpResponse: HttpResponse,
|
httpResponse: HttpResponse,
|
||||||
): Promise<TalerErrorDetail> {
|
): Promise<TalerErrorDetail> {
|
||||||
const errJson = await httpResponse.json();
|
let errJson;
|
||||||
|
try {
|
||||||
|
errJson = await httpResponse.json();
|
||||||
|
} catch (e: any) {
|
||||||
|
throw TalerError.fromDetail(
|
||||||
|
TalerErrorCode.WALLET_RECEIVED_MALFORMED_RESPONSE,
|
||||||
|
{
|
||||||
|
requestUrl: httpResponse.requestUrl,
|
||||||
|
requestMethod: httpResponse.requestMethod,
|
||||||
|
httpStatusCode: httpResponse.status,
|
||||||
|
validationError: e.toString(),
|
||||||
|
},
|
||||||
|
"Couldn't parse JSON format from error response",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const talerErrorCode = errJson.code;
|
const talerErrorCode = errJson.code;
|
||||||
if (typeof talerErrorCode !== "number") {
|
if (typeof talerErrorCode !== "number") {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
@ -163,7 +178,21 @@ export async function readTalerErrorResponse(
|
|||||||
export async function readUnexpectedResponseDetails(
|
export async function readUnexpectedResponseDetails(
|
||||||
httpResponse: HttpResponse,
|
httpResponse: HttpResponse,
|
||||||
): Promise<TalerErrorDetail> {
|
): Promise<TalerErrorDetail> {
|
||||||
const errJson = await httpResponse.json();
|
let errJson;
|
||||||
|
try {
|
||||||
|
errJson = await httpResponse.json();
|
||||||
|
} catch (e: any) {
|
||||||
|
throw TalerError.fromDetail(
|
||||||
|
TalerErrorCode.WALLET_RECEIVED_MALFORMED_RESPONSE,
|
||||||
|
{
|
||||||
|
requestUrl: httpResponse.requestUrl,
|
||||||
|
requestMethod: httpResponse.requestMethod,
|
||||||
|
httpStatusCode: httpResponse.status,
|
||||||
|
validationError: e.toString(),
|
||||||
|
},
|
||||||
|
"Couldn't parse JSON format from error response",
|
||||||
|
);
|
||||||
|
}
|
||||||
const talerErrorCode = errJson.code;
|
const talerErrorCode = errJson.code;
|
||||||
if (typeof talerErrorCode !== "number") {
|
if (typeof talerErrorCode !== "number") {
|
||||||
return makeErrorDetail(
|
return makeErrorDetail(
|
||||||
@ -198,7 +227,21 @@ export async function readSuccessResponseJsonOrErrorCode<T>(
|
|||||||
talerErrorResponse: await readTalerErrorResponse(httpResponse),
|
talerErrorResponse: await readTalerErrorResponse(httpResponse),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
const respJson = await httpResponse.json();
|
let respJson;
|
||||||
|
try {
|
||||||
|
respJson = await httpResponse.json();
|
||||||
|
} catch (e: any) {
|
||||||
|
throw TalerError.fromDetail(
|
||||||
|
TalerErrorCode.WALLET_RECEIVED_MALFORMED_RESPONSE,
|
||||||
|
{
|
||||||
|
requestUrl: httpResponse.requestUrl,
|
||||||
|
requestMethod: httpResponse.requestMethod,
|
||||||
|
httpStatusCode: httpResponse.status,
|
||||||
|
validationError: e.toString(),
|
||||||
|
},
|
||||||
|
"Couldn't parse JSON format from response",
|
||||||
|
);
|
||||||
|
}
|
||||||
let parsedResponse: T;
|
let parsedResponse: T;
|
||||||
try {
|
try {
|
||||||
parsedResponse = codec.decode(respJson);
|
parsedResponse = codec.decode(respJson);
|
||||||
@ -267,7 +310,22 @@ export async function readSuccessResponseTextOrErrorCode<T>(
|
|||||||
httpResponse: HttpResponse,
|
httpResponse: HttpResponse,
|
||||||
): Promise<ResponseOrError<string>> {
|
): Promise<ResponseOrError<string>> {
|
||||||
if (!(httpResponse.status >= 200 && httpResponse.status < 300)) {
|
if (!(httpResponse.status >= 200 && httpResponse.status < 300)) {
|
||||||
const errJson = await httpResponse.json();
|
let errJson;
|
||||||
|
try {
|
||||||
|
errJson = await httpResponse.json();
|
||||||
|
} catch (e: any) {
|
||||||
|
throw TalerError.fromDetail(
|
||||||
|
TalerErrorCode.WALLET_RECEIVED_MALFORMED_RESPONSE,
|
||||||
|
{
|
||||||
|
requestUrl: httpResponse.requestUrl,
|
||||||
|
requestMethod: httpResponse.requestMethod,
|
||||||
|
httpStatusCode: httpResponse.status,
|
||||||
|
validationError: e.toString(),
|
||||||
|
},
|
||||||
|
"Couldn't parse JSON format from error response",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const talerErrorCode = errJson.code;
|
const talerErrorCode = errJson.code;
|
||||||
if (typeof talerErrorCode !== "number") {
|
if (typeof talerErrorCode !== "number") {
|
||||||
throw TalerError.fromDetail(
|
throw TalerError.fromDetail(
|
||||||
@ -296,7 +354,22 @@ export async function checkSuccessResponseOrThrow(
|
|||||||
httpResponse: HttpResponse,
|
httpResponse: HttpResponse,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
if (!(httpResponse.status >= 200 && httpResponse.status < 300)) {
|
if (!(httpResponse.status >= 200 && httpResponse.status < 300)) {
|
||||||
const errJson = await httpResponse.json();
|
let errJson;
|
||||||
|
try {
|
||||||
|
errJson = await httpResponse.json();
|
||||||
|
} catch (e: any) {
|
||||||
|
throw TalerError.fromDetail(
|
||||||
|
TalerErrorCode.WALLET_RECEIVED_MALFORMED_RESPONSE,
|
||||||
|
{
|
||||||
|
requestUrl: httpResponse.requestUrl,
|
||||||
|
requestMethod: httpResponse.requestMethod,
|
||||||
|
httpStatusCode: httpResponse.status,
|
||||||
|
validationError: e.toString(),
|
||||||
|
},
|
||||||
|
"Couldn't parse JSON format from error response",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const talerErrorCode = errJson.code;
|
const talerErrorCode = errJson.code;
|
||||||
if (typeof talerErrorCode !== "number") {
|
if (typeof talerErrorCode !== "number") {
|
||||||
throw TalerError.fromDetail(
|
throw TalerError.fromDetail(
|
||||||
|
Loading…
Reference in New Issue
Block a user