Skip to content

Commit

Permalink
exceptions hierarchy. framework internals exceptions automatically lo…
Browse files Browse the repository at this point in the history
…gged thrown even if get caught. (issue fix #2)
  • Loading branch information
ahmetb committed Jun 1, 2011
1 parent b90656f commit 16a888f
Show file tree
Hide file tree
Showing 41 changed files with 164 additions and 339 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,9 @@

import org.orman.exception.OrmanException;

@SuppressWarnings("serial")
public class DatasourceConnectionException extends OrmanException {
private static final String format = "Datasource connection error: %s";

private String err;

public DatasourceConnectionException(String err){
this.err = err;
}

@Override
public String getMessage() {
return String.format(format, this.err);
super("Datasource connection error. Unable to establish connection to database: %s", err);
}
}
12 changes: 2 additions & 10 deletions src/org/orman/datasource/exception/QueryExecutionException.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@

import org.orman.exception.OrmanException;

@SuppressWarnings("serial")
public class QueryExecutionException extends OrmanException {

private static final String format = "Query execution error: %s";

private String err;

public QueryExecutionException(String err){
this.err = err;
}

@Override
public String getMessage() {
return String.format(format, this.err);
super("Query execution error: %s", err);
}
}
3 changes: 2 additions & 1 deletion src/org/orman/exception/FeatureNotImplementedException.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.orman.exception;

public class FeatureNotImplementedException extends RuntimeException {
@SuppressWarnings("serial")
public class FeatureNotImplementedException extends OrmanException {

public FeatureNotImplementedException(String message) {
super(message);
Expand Down
14 changes: 12 additions & 2 deletions src/org/orman/exception/OrmanException.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package org.orman.exception;

import org.orman.util.logging.Log;

@SuppressWarnings("serial")
public class OrmanException extends RuntimeException {

public OrmanException(){}

public OrmanException(String message) {
super(message);
public OrmanException(String message, Object... parameters) {
super(String.format(message, parameters));
}

@Override
public String getMessage() {
// defer logging of exception to the end.
Log.error(super.getMessage());
return super.getMessage();
}

}
10 changes: 10 additions & 0 deletions src/org/orman/exception/OrmanMappingException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.orman.exception;


@SuppressWarnings("serial")
public class OrmanMappingException extends OrmanException {

public OrmanMappingException(String message, Object... parameters){
super(String.format(message, parameters));
}
}
20 changes: 5 additions & 15 deletions src/org/orman/mapper/MappingSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,8 @@ public static void registerPackage(String packageName) {

if (annotatedClasses == null || annotatedClasses.size() == 0) {
// no @Entity-annotated classes found in package.
AnnotatedClassNotFoundInPackageException ex = new AnnotatedClassNotFoundInPackageException(
throw new AnnotatedClassNotFoundInPackageException(
packageName);
Log.error(ex.getMessage());
throw ex;
}

// still throws exception
Expand All @@ -101,9 +99,7 @@ public static void registerPackage(String packageName) {
*/
public static void registerEntity(Class<?> entityClass) {
if (sessionStarted) {
MappingSessionAlreadyStartedException ex = new MappingSessionAlreadyStartedException();
Log.error(ex.getMessage());
throw ex;
throw new MappingSessionAlreadyStartedException();
}

Entity e = new Entity(entityClass);
Expand All @@ -114,9 +110,7 @@ public static void registerEntity(Class<?> entityClass) {

protected static void registerSyntheticEntity(Entity e) {
if (sessionStarted) {
MappingSessionAlreadyStartedException ex = new MappingSessionAlreadyStartedException();
Log.error(ex.getMessage());
throw ex;
throw new MappingSessionAlreadyStartedException();
}

Log.info("Registering synthetic entity %s --> %s.", e.getOriginalName(), e.getGeneratedName());
Expand Down Expand Up @@ -211,9 +205,7 @@ public static Entity getEntityByClassName(String className) {
*/
public static void start() {
if (sessionStarted) {
MappingSessionAlreadyStartedException e = new MappingSessionAlreadyStartedException();
Log.error(e.getMessage());
throw e;
throw new MappingSessionAlreadyStartedException();
}
startNoCheck();
}
Expand Down Expand Up @@ -267,9 +259,7 @@ private static void startNoCheck() {
}

if (db == null) {
NoDatabaseRegisteredException e = new NoDatabaseRegisteredException();
Log.error(e.getMessage());
throw e;
throw new NoDatabaseRegisteredException();
}

// set custom SQL grammar provider binding
Expand Down
6 changes: 3 additions & 3 deletions src/org/orman/mapper/ModelQuery.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.orman.mapper;

import org.orman.exception.OrmanMappingException;
import org.orman.mapper.exception.EntityNotFoundException;
import org.orman.mapper.exception.FieldNotFoundException;
import org.orman.mapper.exception.GenericOrmanException;
import org.orman.sql.Criterion;
import org.orman.sql.JoinType;
import org.orman.sql.Query;
Expand Down Expand Up @@ -195,7 +195,7 @@ private Field parseField(String expr) {
int s = expr.indexOf('.');

if (s < 0)
throw new GenericOrmanException(String.format(
throw new OrmanMappingException(String.format(
"Invalid field format: `%s`. (e.g. EntityName.fieldName)",
expr));

Expand All @@ -219,7 +219,7 @@ private Entity parseEntity(String expr) {
int s = expr.indexOf('.');

if (s < 0)
throw new GenericOrmanException(String.format(
throw new OrmanMappingException(String.format(
"Invalid field format: `%s`. (e.g. EntityName.fieldName)",
expr));

Expand Down
8 changes: 2 additions & 6 deletions src/org/orman/mapper/PersistenceSchemeMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,15 @@ public void checkConflictingFields(Entity e) {
if (f.getType() == null && !MappingSession.entityExists(f.getClazz())
//&& f.getType().equals(List.class) //TODO seems unnecessary
) {
UnmappedDataTypeException ex = new UnmappedDataTypeException(f.getOriginalName(), f
throw new UnmappedDataTypeException(f.getOriginalName(), f
.getClazz().getName());
Log.error(ex.getMessage());
throw ex;
}

// conflicting column name bindings
for (Field g : e.getFields()) {
if (f != g && f.equals(g)) {
DuplicateColumnNamesException ex = new DuplicateColumnNamesException(
throw new DuplicateColumnNamesException(
f.getOriginalName(), g.getOriginalName());
Log.error(ex.getMessage());
throw ex;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/org/orman/mapper/ReverseMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ private static Object smartCasting(Object value, Class<?> desired) {
}
return d;
} catch (ParseException e) {
Log.error("The following could not be parsed: " + value.toString());
Log.error("The following date could not be parsed: " + value.toString());
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package org.orman.mapper.exception;

public class AnnotatedClassNotFoundInPackageException extends RuntimeException {
private String packageName;

import org.orman.exception.OrmanMappingException;

@SuppressWarnings("serial")
public class AnnotatedClassNotFoundInPackageException extends
OrmanMappingException {
public AnnotatedClassNotFoundInPackageException(String packageName) {
this.packageName = packageName;
}

public String getMessage() {
return String.format("Could not find any entity marked class in \'%s\'",packageName);
super("Could not find any entity marked class in \'%s\'", packageName);
}
}
14 changes: 4 additions & 10 deletions src/org/orman/mapper/exception/DuplicateColumnNamesException.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package org.orman.mapper.exception;

import org.orman.exception.OrmanMappingException;


@SuppressWarnings("serial")
public class DuplicateColumnNamesException extends RuntimeException {
private static String message = "Unable to map more than one fields to the same column name: %s, %s.";
private String s1,s2;

public class DuplicateColumnNamesException extends OrmanMappingException {
public DuplicateColumnNamesException(String s1, String s2){
this.s1 = s1;
this.s2 = s2;
}

public String getMessage() {
return String.format(message, s1, s2);
super("Unable to map more than one fields to the same column name: %s, %s.", s1, s2);
}
}
14 changes: 4 additions & 10 deletions src/org/orman/mapper/exception/DuplicateTableNamesException.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package org.orman.mapper.exception;

import org.orman.exception.OrmanMappingException;


@SuppressWarnings("serial")
public class DuplicateTableNamesException extends RuntimeException {
private static String message = "Unable to map more than one entities to the same table name: %s, %s.";
private String s1,s2;

public class DuplicateTableNamesException extends OrmanMappingException {
public DuplicateTableNamesException(String s1, String s2){
this.s1 = s1;
this.s2 = s2;
}

public String getMessage() {
return String.format(message, s1, s2);
super("Unable to map more than one entities to the same table name: %s, %s.", s1, s2);
}
}
15 changes: 4 additions & 11 deletions src/org/orman/mapper/exception/EntityNotFoundException.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package org.orman.mapper.exception;

import org.orman.exception.OrmanMappingException;


@SuppressWarnings("serial")
public class EntityNotFoundException extends RuntimeException {
private static final String message = "The entity type `%s` could not be found in MappingSession context. Register this @Entity to MappingSession first.";

private String type;

public class EntityNotFoundException extends OrmanMappingException {
public EntityNotFoundException(String type){
this.type = type;
}

@Override
public String getMessage() {
return String.format(message, this.type);
super("The entity type %s could not be found in MappingSession context. Register this @Entity to MappingSession first.", type);
}
}
14 changes: 4 additions & 10 deletions src/org/orman/mapper/exception/FieldNotFoundException.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package org.orman.mapper.exception;

import org.orman.exception.OrmanMappingException;


@SuppressWarnings("serial")
public class FieldNotFoundException extends RuntimeException {
private static String message = "Could not find property (field) `%s` in %s.";
private String f,c;

public class FieldNotFoundException extends OrmanMappingException {
public FieldNotFoundException(String type, String field){
this.f = field;
this.c = type;
}

public String getMessage() {
return String.format(message, f, c);
super("Could not find property (field) `%s` in %s.", field, type);
}
}
16 changes: 0 additions & 16 deletions src/org/orman/mapper/exception/GenericOrmanException.java

This file was deleted.

15 changes: 4 additions & 11 deletions src/org/orman/mapper/exception/IndexNotFoundException.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package org.orman.mapper.exception;

import org.orman.exception.OrmanMappingException;


@SuppressWarnings("serial")
public class IndexNotFoundException extends RuntimeException {
private static final String message = "Unable to create index from a non-indexed field. Use @Index annotation on the following field: %s";

private String s;

public class IndexNotFoundException extends OrmanMappingException {
public IndexNotFoundException(String name){
this.s = name;
}

@Override
public String getMessage() {
return String.format(message, this.s);
super("Unable to create index from a non-indexed field. Use @Index annotation on the following field: %s", name);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.orman.mapper.exception;

import org.orman.exception.OrmanMappingException;


@SuppressWarnings("serial")
public class MappingSessionAlreadyStartedException extends RuntimeException {
@Override
public String getMessage() {
String message = "Mapping session has already been started. Do not use start method more than once.";
return message;
public class MappingSessionAlreadyStartedException extends OrmanMappingException {
public MappingSessionAlreadyStartedException(){
super("Mapping session has already been started. Do not use start method more than once.");
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.orman.mapper.exception;

import org.orman.exception.OrmanMappingException;


@SuppressWarnings("serial")
public class MappingSessionNotStartedException extends RuntimeException {
@Override
public String getMessage() {
String message = "Mapping session has not been started. Please start() session before using this method.";
return message;
public class MappingSessionNotStartedException extends OrmanMappingException {
public MappingSessionNotStartedException(){
super("Mapping session has not been started. Please start() session before using this method.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@

@SuppressWarnings("serial")
public class NoDatabaseRegisteredException extends RuntimeException {

public static final String message = "No database registered to the mapping session. Use registerDatabase() once.";
@Override
public String getMessage() {
return message;
public NoDatabaseRegisteredException(){
super("No database registered to the mapping session. Use registerDatabase() once.");
}

}
Loading

0 comments on commit 16a888f

Please sign in to comment.