Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/main/java/com/backend/coapp/exception/ApiException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.backend.coapp.exception;

import org.springframework.http.HttpStatus;

/** Interface for exception from API call */
public abstract class ApiException extends RuntimeException {
/**
* getStatus
*
* @return HTTPS Status
*/
public abstract HttpStatus getStatus();

/**
* getErrorCode
*
* @return custom error code
*/
public abstract Object getErrorCode();

protected ApiException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
package com.backend.coapp.exception.application;

public class ApplicationNotFoundException extends RuntimeException {
import com.backend.coapp.exception.ApiException;
import com.backend.coapp.model.enumeration.ApplicationErrorCode;
import org.springframework.http.HttpStatus;

/** This exception will be thrown when application not found. */
public class ApplicationNotFoundException extends ApiException {
public ApplicationNotFoundException() {
super("Could not find application");
}

@Override
public HttpStatus getStatus() {
return HttpStatus.NOT_FOUND;
}

@Override
public Object getErrorCode() {
return ApplicationErrorCode.APPLICATION_NOT_FOUND;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
package com.backend.coapp.exception.application;

public class ApplicationNotOwnedException extends RuntimeException {
import com.backend.coapp.exception.ApiException;
import com.backend.coapp.model.enumeration.ApplicationErrorCode;
import org.springframework.http.HttpStatus;

/** This exception will be thrown will the application is not belong to a user. */
public class ApplicationNotOwnedException extends ApiException {
public ApplicationNotOwnedException() {
super("Application is not owned by user. No operation is allowed.");
}

@Override
public HttpStatus getStatus() {
return HttpStatus.FORBIDDEN;
}

@Override
public Object getErrorCode() {
return ApplicationErrorCode.APPLICATION_NOT_OWN;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
package com.backend.coapp.exception.application;

public class ApplicationServiceFailException extends RuntimeException {
import com.backend.coapp.exception.ApiException;
import com.backend.coapp.model.enumeration.SystemErrorCode;
import org.springframework.http.HttpStatus;

/** This exception will be thrown when application service fail (internal) */
public class ApplicationServiceFailException extends ApiException {
public ApplicationServiceFailException(String message) {
super(message);
}

@Override
public HttpStatus getStatus() {
return HttpStatus.INTERNAL_SERVER_ERROR;
}

@Override
public Object getErrorCode() {
return SystemErrorCode.INTERNAL_ERROR;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
package com.backend.coapp.exception.application;

public class DuplicateApplicationException extends RuntimeException {
import com.backend.coapp.exception.ApiException;
import com.backend.coapp.model.enumeration.ApplicationErrorCode;
import org.springframework.http.HttpStatus;

/** This application will be thrown when the application already exist */
public class DuplicateApplicationException extends ApiException {
public DuplicateApplicationException(String jobTitle, String companyId) {
super(
String.format(
"User has already created an application for '%s' at company %s", jobTitle, companyId));
}

@Override
public HttpStatus getStatus() {
return HttpStatus.CONFLICT;
}

@Override
public Object getErrorCode() {
return ApplicationErrorCode.DUPLICATE_APPLICATION;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
package com.backend.coapp.exception.application;

public class NoChangesDetectedException extends RuntimeException {
import com.backend.coapp.exception.ApiException;
import com.backend.coapp.model.enumeration.ApplicationErrorCode;
import org.springframework.http.HttpStatus;

/** This exception will be thrown when no changed detected in updated application */
public class NoChangesDetectedException extends ApiException {
public NoChangesDetectedException(String message) {
super(message);
}

@Override
public HttpStatus getStatus() {
return HttpStatus.BAD_REQUEST;
}

@Override
public Object getErrorCode() {
return ApplicationErrorCode.NO_CHANGE_DETECTED_TO_UPDATE;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
package com.backend.coapp.exception.application;

public class UnauthorizedApplicationAccessException extends RuntimeException {
import com.backend.coapp.exception.ApiException;
import com.backend.coapp.model.enumeration.ApplicationErrorCode;
import org.springframework.http.HttpStatus;

/** This exception will be thrown when the user tries to access the unauthorized resource. */
public class UnauthorizedApplicationAccessException extends ApiException {
public UnauthorizedApplicationAccessException(String message) {
super(message);
}

@Override
public HttpStatus getStatus() {
return HttpStatus.FORBIDDEN;
}

@Override
public Object getErrorCode() {
return ApplicationErrorCode.UNAUTHORIZED_APPLICATION_ACCESS;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
package com.backend.coapp.exception.auth;

import com.backend.coapp.exception.ApiException;
import com.backend.coapp.model.enumeration.AuthErrorCode;
import org.springframework.http.HttpStatus;

/**
* This exception will be thrown if a client tries to verify an account that have been already
* verified.
*/
public class AuthAccountAlreadyVerifyException extends RuntimeException {
public class AuthAccountAlreadyVerifyException extends ApiException {
public AuthAccountAlreadyVerifyException() {
super("Account has been verified.");
}

public AuthAccountAlreadyVerifyException(String message) {
super("Account has been verified. " + message);
}

@Override
public HttpStatus getStatus() {
return HttpStatus.METHOD_NOT_ALLOWED;
}

@Override
public Object getErrorCode() {
return AuthErrorCode.ACCOUNT_ALREADY_VERIFIED;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.backend.coapp.exception.auth;

import com.backend.coapp.exception.ApiException;
import com.backend.coapp.model.enumeration.AuthErrorCode;
import org.springframework.http.HttpStatus;

/**
* This exception will be thrown if client tries to request service with an account that not yet
* activate
*/
public class AuthAccountNotYetActivatedException extends RuntimeException {
public class AuthAccountNotYetActivatedException extends ApiException {
public AuthAccountNotYetActivatedException() {
super(
"Account has not been activated yet. Please use verification code to activate the account.");
Expand All @@ -15,4 +19,14 @@ public AuthAccountNotYetActivatedException(String message) {
"Account has not been activated yet. Please use verification code to activate the account. "
+ message);
}

@Override
public HttpStatus getStatus() {
return HttpStatus.UNAUTHORIZED;
}

@Override
public Object getErrorCode() {
return AuthErrorCode.ACCOUNT_NOT_ACTIVATED;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
package com.backend.coapp.exception.auth;

import com.backend.coapp.exception.ApiException;
import com.backend.coapp.model.enumeration.AuthErrorCode;
import org.springframework.http.HttpStatus;

/** This exception will be thrown when a user tries to log in with invalid credential. */
public class AuthBadCredentialException extends RuntimeException {
public class AuthBadCredentialException extends ApiException {
public AuthBadCredentialException() {
super("Incorrect email or password");
}

public AuthBadCredentialException(String message) {
super("Bad credential. " + message);
}

@Override
public HttpStatus getStatus() {
return HttpStatus.UNAUTHORIZED;
}

@Override
public Object getErrorCode() {
return AuthErrorCode.INVALID_EMAIL_OR_PASSWORD;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
package com.backend.coapp.exception.auth;

import com.backend.coapp.exception.ApiException;
import com.backend.coapp.model.enumeration.AuthErrorCode;
import org.springframework.http.HttpStatus;

/**
* This exception will be thrown if client tries to register a new account with an email that has
* been used.
*/
public class AuthEmailAlreadyUsedException extends RuntimeException {
public class AuthEmailAlreadyUsedException extends ApiException {
public AuthEmailAlreadyUsedException() {
super("An account with that email already exists.");
}

public AuthEmailAlreadyUsedException(String message) {
super("An account with that email already exists. " + message);
}

@Override
public HttpStatus getStatus() {
return HttpStatus.CONFLICT;
}

@Override
public Object getErrorCode() {
return AuthErrorCode.EXIST_ACCOUNT_WITH_EMAIL;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
package com.backend.coapp.exception.auth;

import org.springframework.security.core.userdetails.UsernameNotFoundException;
import com.backend.coapp.exception.ApiException;
import com.backend.coapp.model.enumeration.AuthErrorCode;
import org.springframework.http.HttpStatus;

/**
* This exception will be thrown when client tries to access information with unregistered email.
*/
public class AuthEmailNotRegisteredException extends UsernameNotFoundException {
public class AuthEmailNotRegisteredException extends ApiException {
public AuthEmailNotRegisteredException() {
super("Email is not yet registered.");
}

public AuthEmailNotRegisteredException(String message) {
super("Email is not yet registered. " + message);
}

@Override
public HttpStatus getStatus() {
return HttpStatus.BAD_REQUEST;
}

@Override
public Object getErrorCode() {
return AuthErrorCode.EMAIL_NOT_REGISTERED;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
package com.backend.coapp.exception.auth;

import com.backend.coapp.exception.ApiException;
import com.backend.coapp.model.enumeration.AuthErrorCode;
import org.springframework.http.HttpStatus;

/** This exception will be thrown if the provided email is invalid. */
public class EmailInvalidAddressException extends RuntimeException {
public class EmailInvalidAddressException extends ApiException {
public EmailInvalidAddressException() {
super("Invalid email or email not exit.");
}

public EmailInvalidAddressException(String message) {
super("Invalid email or email not exit. " + message);
}

@Override
public HttpStatus getStatus() {
return HttpStatus.BAD_REQUEST;
}

@Override
public Object getErrorCode() {
return AuthErrorCode.INVALID_EMAIL;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.backend.coapp.exception.auth;

import com.backend.coapp.exception.ApiException;
import com.backend.coapp.model.enumeration.SystemErrorCode;
import org.springframework.http.HttpStatus;

/** This exception will be thrown if there is a failure related to JavaMailSender service. */
public class EmailServiceException extends RuntimeException {
public class EmailServiceException extends ApiException {

public EmailServiceException() {
super("JavaMailSender Service Failure.");
Expand All @@ -10,4 +14,14 @@ public EmailServiceException() {
public EmailServiceException(String message) {
super("JavaMailSender Service Failure. " + message);
}

@Override
public HttpStatus getStatus() {
return HttpStatus.INTERNAL_SERVER_ERROR;
}

@Override
public Object getErrorCode() {
return SystemErrorCode.INTERNAL_ERROR;
}
}
Loading
Loading