Better error handling
This commit is contained in:
@ -1,3 +1,13 @@
|
||||
class ValidationError extends Error {
|
||||
constructor(message) {
|
||||
if (!message) {
|
||||
message = "ValidationError";
|
||||
}
|
||||
super(message);
|
||||
this.name = "ValidationError";
|
||||
}
|
||||
}
|
||||
|
||||
class PlayerNotFoundError extends Error {
|
||||
constructor(message) {
|
||||
if (!message) {
|
||||
@ -65,6 +75,10 @@ class FairHopper {
|
||||
headers: this.defaultHeaders,
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
switch (r.status) {
|
||||
case 422:
|
||||
throw new ValidationError();
|
||||
}
|
||||
return await r.json();
|
||||
}
|
||||
|
||||
@ -84,6 +98,8 @@ class FairHopper {
|
||||
throw new PlayerInactiveError();
|
||||
case 404:
|
||||
throw new PlayerNotFoundError();
|
||||
case 422:
|
||||
throw new ValidationError();
|
||||
}
|
||||
return await r.json();
|
||||
}
|
||||
@ -117,6 +133,8 @@ class FairHopper {
|
||||
throw new PlayerNotFoundError();
|
||||
case 409:
|
||||
throw new PositionError();
|
||||
case 422:
|
||||
throw new ValidationError();
|
||||
}
|
||||
return await r.json();
|
||||
}
|
||||
@ -128,6 +146,17 @@ class FairHopper {
|
||||
return await r.json();
|
||||
}
|
||||
|
||||
async getProduct(productId) {
|
||||
const r = await fetch(this.formatUrl(`/products/${productId}`), {
|
||||
headers: this.defaultHeaders,
|
||||
});
|
||||
switch (r.status) {
|
||||
case 422:
|
||||
throw new ValidationError();
|
||||
}
|
||||
return await r.json();
|
||||
}
|
||||
|
||||
async purchaseProduct(playerId, productId) {
|
||||
const url = this.formatUrl(`/player/${playerId}/product/purchase`);
|
||||
const postData = {
|
||||
@ -145,6 +174,8 @@ class FairHopper {
|
||||
throw new PlayerNotFoundError();
|
||||
case 409:
|
||||
throw new PositionError();
|
||||
case 422:
|
||||
throw new ValidationError();
|
||||
}
|
||||
return await r.json();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user