Skip to content
Snippets Groups Projects
Commit 80e02713 authored by BEAUVAIS ANTOINE's avatar BEAUVAIS ANTOINE
Browse files

Proper error management & prices in BigDecimal and double.

parent c82e0285
No related merge requests found
/*
* CONTRAT DE LICENCE DE LOGICIEL LIBRE CeCILL-B
* https://cecill.info/licences/Licence_CeCILL-B_V1-fr.html
*/
package fr.unistra.sil.erp.back.api.controller;
import fr.unistra.sil.erp.back.api.model.ErrorMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* Handles API exceptions.
* @author BEAUVAIS ANTOINE
*/
@RestControllerAdvice
public class ApiErrorHandler {
@ExceptionHandler(ApiServerErrorException.class)
public ResponseEntity<Object> handleServerError(Exception ex,
HttpServletRequest request, HttpServletResponse response)
{
return new ResponseEntity<>(new ErrorMessage(ex.getMessage()),
HttpStatus.INTERNAL_SERVER_ERROR);
}
}
......@@ -24,9 +24,10 @@ public class ApiRetrieveInfoController {
/**
* Returns an Item as a JSON response.
* @return an Item.
* @throws fr.unistra.sil.erp.back.api.controller.ApiServerErrorException
*/
@GetMapping("/erp-back/api/v1/retrieveInfo")
public List<Item> retrieveInfo()
public Object retrieveInfo() throws ApiServerErrorException
{
DatabaseInterface db;
try {
......@@ -34,12 +35,12 @@ public class ApiRetrieveInfoController {
} catch (DatabaseConnectionException ex) {
Logger.getLogger(ApiRetrieveInfoController.class.getName()).log(
Level.SEVERE, "Database failure.", ex);
throw new ApiServerErrorController("Connection to database failed");
throw new ApiServerErrorException("Failed to connect to database.");
}
List<Item> res = db.getAllItems();
if(res == null)
throw new ApiServerErrorController("Failed to retrieve list.");
throw new ApiServerErrorException("Failed to query info.");
return res;
}
......
......@@ -4,19 +4,14 @@
*/
package fr.unistra.sil.erp.back.api.controller;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
* Returns HTTP 500 error page.
* @author BEAUVAIS ANTOINE <antoine.beauvais@etu.unistra.fr>
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public class ApiServerErrorController extends RuntimeException {
public ApiServerErrorController(String errMsg)
public class ApiServerErrorException extends Exception {
public ApiServerErrorException(String errMsg)
{
super(errMsg);
}
}
/*
* CONTRAT DE LICENCE DE LOGICIEL LIBRE CeCILL-B
* https://cecill.info/licences/Licence_CeCILL-B_V1-fr.html
*/
package fr.unistra.sil.erp.back.api.model;
/**
* JSON error message.
* @author BEAUVAIS ANTOINE
*/
public class ErrorMessage {
private final String message;
public ErrorMessage(String message)
{
this.message = message;
}
public String getMessage()
{
return this.message;
}
}
......@@ -4,6 +4,8 @@
*/
package fr.unistra.sil.erp.back.api.model;
import java.math.BigDecimal;
/**
* Item description.
* @author BEAUVAIS ANTOINE <antoine.beauvais@etu.unistra.fr>
......@@ -23,12 +25,12 @@ public class Item {
/**
* The product's regular price.
*/
private final double price;
private final BigDecimal price;
/**
* The product's price for subscribers.
*/
private final double subscriberPrice;
private final BigDecimal subscriberPrice;
/**
* Class constructor.
......@@ -37,8 +39,8 @@ public class Item {
* @param price The item's price.
* @param subscriberPrice The item's price for subscribers.
*/
public Item(int ref, String name, double price,
double subscriberPrice)
public Item(int ref, String name, BigDecimal price,
BigDecimal subscriberPrice)
{
this.ref = ref;
this.name = name;
......@@ -68,7 +70,7 @@ public class Item {
* Returns the current item's price.
* @return the price.
*/
public double getPrice()
public BigDecimal getPrice()
{
return this.price;
}
......@@ -77,7 +79,7 @@ public class Item {
* Returns the current item's price for subscribers.
* @return the price for subscribers.
*/
public double getSubscriberPrice()
public BigDecimal getSubscriberPrice()
{
return this.subscriberPrice;
}
......
......@@ -64,8 +64,8 @@ public class DatabaseSQLiteImpl implements DatabaseInterface {
while(rs.next())
{
Item item = new Item(rs.getInt("ref"), rs.getString("name"),
rs.getDouble("price"),
rs.getDouble("subscriberPrice"));
rs.getBigDecimal("price"),
rs.getBigDecimal("subscriberPrice"));
res.add(item);
}
} catch (SQLException ex) {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment