From bf0490de395a529e4f5117809798c0effe353810 Mon Sep 17 00:00:00 2001 From: michada Date: Sat, 15 Feb 2014 23:58:30 +0100 Subject: [PATCH] Better error management in people DAO and REST API. PeopleDAO functions now throw an IllegalArgumentException when an invalid id or a null name or surname are provided. People's REST API modified to return a "Bad Request" status when an IllegalArgumentException is catched. --- .../java/es/uvigo/esei/daa/dao/PeopleDAO.java | 26 ++++++++++++++----- .../java/es/uvigo/esei/daa/rest/People.java | 16 ++++++++++++ 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/main/java/es/uvigo/esei/daa/dao/PeopleDAO.java b/src/main/java/es/uvigo/esei/daa/dao/PeopleDAO.java index b02a754..d0d932f 100644 --- a/src/main/java/es/uvigo/esei/daa/dao/PeopleDAO.java +++ b/src/main/java/es/uvigo/esei/daa/dao/PeopleDAO.java @@ -11,7 +11,8 @@ import java.util.List; import es.uvigo.esei.daa.entities.Person; public class PeopleDAO extends DAO { - public Person get(int id) throws DAOException { + public Person get(int id) + throws DAOException, IllegalArgumentException { try (final Connection conn = this.getConnection()) { final String query = "SELECT * FROM people WHERE id=?"; @@ -26,7 +27,7 @@ public class PeopleDAO extends DAO { result.getString("surname") ); } else { - throw new DAOException("Person not found"); + throw new IllegalArgumentException("Invalid id"); } } } @@ -57,7 +58,8 @@ public class PeopleDAO extends DAO { } } - public void delete(int id) throws DAOException { + public void delete(int id) + throws DAOException, IllegalArgumentException { try (final Connection conn = this.getConnection()) { final String query = "DELETE FROM people WHERE id=?"; @@ -65,7 +67,7 @@ public class PeopleDAO extends DAO { statement.setInt(1, id); if (statement.executeUpdate() != 1) { - throw new SQLException("Error inserting value"); + throw new IllegalArgumentException("Invalid id"); } } } catch (SQLException e) { @@ -73,7 +75,12 @@ public class PeopleDAO extends DAO { } } - public Person modify(int id, String name, String surname) throws DAOException { + public Person modify(int id, String name, String surname) + throws DAOException, IllegalArgumentException { + if (name == null || surname == null) { + throw new IllegalArgumentException("name and surname can't be null"); + } + try (final Connection conn = this.getConnection()) { final String query = "UPDATE people SET name=?, surname=? WHERE id=?"; @@ -85,7 +92,7 @@ public class PeopleDAO extends DAO { if (statement.executeUpdate() == 1) { return new Person(id, name, surname); } else { - throw new SQLException("Error inserting value"); + throw new IllegalArgumentException("name and surname can't be null"); } } } catch (SQLException e) { @@ -93,7 +100,12 @@ public class PeopleDAO extends DAO { } } - public Person add(String name, String surname) throws DAOException { + public Person add(String name, String surname) + throws DAOException, IllegalArgumentException { + if (name == null || surname == null) { + throw new IllegalArgumentException("name and surname can't be null"); + } + try (final Connection conn = this.getConnection()) { final String query = "INSERT INTO people VALUES(null, ?, ?)"; diff --git a/src/main/java/es/uvigo/esei/daa/rest/People.java b/src/main/java/es/uvigo/esei/daa/rest/People.java index ada32ca..8bab204 100644 --- a/src/main/java/es/uvigo/esei/daa/rest/People.java +++ b/src/main/java/es/uvigo/esei/daa/rest/People.java @@ -40,6 +40,10 @@ public class People { ) { try { return Response.ok(this.dao.get(id), MediaType.APPLICATION_JSON).build(); + } catch (IllegalArgumentException iae) { + iae.printStackTrace(); + return Response.status(Response.Status.BAD_REQUEST) + .entity(iae.getMessage()).build(); } catch (DAOException e) { e.printStackTrace(); return Response.serverError().entity(e.getMessage()).build(); @@ -55,6 +59,10 @@ public class People { this.dao.delete(id); return Response.ok(id).build(); + } catch (IllegalArgumentException iae) { + iae.printStackTrace(); + return Response.status(Response.Status.BAD_REQUEST) + .entity(iae.getMessage()).build(); } catch (DAOException e) { e.printStackTrace(); return Response.serverError().entity(e.getMessage()).build(); @@ -70,6 +78,10 @@ public class People { ) { try { return Response.ok(this.dao.modify(id, name, surname)).build(); + } catch (IllegalArgumentException iae) { + iae.printStackTrace(); + return Response.status(Response.Status.BAD_REQUEST) + .entity(iae.getMessage()).build(); } catch (DAOException e) { e.printStackTrace(); return Response.serverError().entity(e.getMessage()).build(); @@ -83,6 +95,10 @@ public class People { ) { try { return Response.ok(this.dao.add(name, surname)).build(); + } catch (IllegalArgumentException iae) { + iae.printStackTrace(); + return Response.status(Response.Status.BAD_REQUEST) + .entity(iae.getMessage()).build(); } catch (DAOException e) { e.printStackTrace(); return Response.serverError().entity(e.getMessage()).build(); -- 2.18.1