Commit bf0490de authored by michada's avatar michada

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.
parent 872d0b42
...@@ -11,7 +11,8 @@ import java.util.List; ...@@ -11,7 +11,8 @@ import java.util.List;
import es.uvigo.esei.daa.entities.Person; import es.uvigo.esei.daa.entities.Person;
public class PeopleDAO extends DAO { 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()) { try (final Connection conn = this.getConnection()) {
final String query = "SELECT * FROM people WHERE id=?"; final String query = "SELECT * FROM people WHERE id=?";
...@@ -26,7 +27,7 @@ public class PeopleDAO extends DAO { ...@@ -26,7 +27,7 @@ public class PeopleDAO extends DAO {
result.getString("surname") result.getString("surname")
); );
} else { } else {
throw new DAOException("Person not found"); throw new IllegalArgumentException("Invalid id");
} }
} }
} }
...@@ -57,7 +58,8 @@ public class PeopleDAO extends DAO { ...@@ -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()) { try (final Connection conn = this.getConnection()) {
final String query = "DELETE FROM people WHERE id=?"; final String query = "DELETE FROM people WHERE id=?";
...@@ -65,7 +67,7 @@ public class PeopleDAO extends DAO { ...@@ -65,7 +67,7 @@ public class PeopleDAO extends DAO {
statement.setInt(1, id); statement.setInt(1, id);
if (statement.executeUpdate() != 1) { if (statement.executeUpdate() != 1) {
throw new SQLException("Error inserting value"); throw new IllegalArgumentException("Invalid id");
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
...@@ -73,7 +75,12 @@ public class PeopleDAO extends DAO { ...@@ -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()) { try (final Connection conn = this.getConnection()) {
final String query = "UPDATE people SET name=?, surname=? WHERE id=?"; final String query = "UPDATE people SET name=?, surname=? WHERE id=?";
...@@ -85,7 +92,7 @@ public class PeopleDAO extends DAO { ...@@ -85,7 +92,7 @@ public class PeopleDAO extends DAO {
if (statement.executeUpdate() == 1) { if (statement.executeUpdate() == 1) {
return new Person(id, name, surname); return new Person(id, name, surname);
} else { } else {
throw new SQLException("Error inserting value"); throw new IllegalArgumentException("name and surname can't be null");
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
...@@ -93,7 +100,12 @@ public class PeopleDAO extends DAO { ...@@ -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()) { try (final Connection conn = this.getConnection()) {
final String query = "INSERT INTO people VALUES(null, ?, ?)"; final String query = "INSERT INTO people VALUES(null, ?, ?)";
......
...@@ -40,6 +40,10 @@ public class People { ...@@ -40,6 +40,10 @@ public class People {
) { ) {
try { try {
return Response.ok(this.dao.get(id), MediaType.APPLICATION_JSON).build(); 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) { } catch (DAOException e) {
e.printStackTrace(); e.printStackTrace();
return Response.serverError().entity(e.getMessage()).build(); return Response.serverError().entity(e.getMessage()).build();
...@@ -55,6 +59,10 @@ public class People { ...@@ -55,6 +59,10 @@ public class People {
this.dao.delete(id); this.dao.delete(id);
return Response.ok(id).build(); return Response.ok(id).build();
} catch (IllegalArgumentException iae) {
iae.printStackTrace();
return Response.status(Response.Status.BAD_REQUEST)
.entity(iae.getMessage()).build();
} catch (DAOException e) { } catch (DAOException e) {
e.printStackTrace(); e.printStackTrace();
return Response.serverError().entity(e.getMessage()).build(); return Response.serverError().entity(e.getMessage()).build();
...@@ -70,6 +78,10 @@ public class People { ...@@ -70,6 +78,10 @@ public class People {
) { ) {
try { try {
return Response.ok(this.dao.modify(id, name, surname)).build(); 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) { } catch (DAOException e) {
e.printStackTrace(); e.printStackTrace();
return Response.serverError().entity(e.getMessage()).build(); return Response.serverError().entity(e.getMessage()).build();
...@@ -83,6 +95,10 @@ public class People { ...@@ -83,6 +95,10 @@ public class People {
) { ) {
try { try {
return Response.ok(this.dao.add(name, surname)).build(); 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) { } catch (DAOException e) {
e.printStackTrace(); e.printStackTrace();
return Response.serverError().entity(e.getMessage()).build(); return Response.serverError().entity(e.getMessage()).build();
......
Markdown is supported
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