Skip to content

Instantly share code, notes, and snippets.

@aoudiamoncef
Created July 9, 2026 13:32
Show Gist options
  • Select an option

  • Save aoudiamoncef/24c78f24c0ff2fb5507b6799e2196909 to your computer and use it in GitHub Desktop.

Select an option

Save aoudiamoncef/24c78f24c0ff2fb5507b6799e2196909 to your computer and use it in GitHub Desktop.
Global Application Exception
package com.labs.app.api.domain.exception;
import java.util.function.Supplier;
public class AppException extends RuntimeException {
private final int errorCode;
public AppException(final String reason) {
super(reason);
this.errorCode = -1;
}
public AppException(final int code, final String reason) {
super(reason);
this.errorCode = code;
}
public AppException(final String message, final Throwable cause) {
super(message, cause);
this.errorCode = -1;
}
public AppException(final String message, final int code, final Throwable cause) {
super(message, cause);
this.errorCode = code;
}
public int getErrorCode() {
return errorCode;
}
public static Supplier<AppException> toSupplier(final String reason) {
return () -> new AppException(reason);
}
public static Supplier<AppException> toSupplier(final int code, final String reason) {
return () -> new AppException(code, reason);
}
public static Runnable toRunnable(final int code, final String reason) {
return () -> {
throw new AppException(code, reason);
};
}
public static Supplier<AppException> toSupplier(final String reason, final Throwable cause) {
return () -> new AppException(reason, cause);
}
public static Supplier<AppException> toSupplier(final String reason, final int code, final Throwable cause) {
return () -> new AppException(reason, code, cause);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment