// Path: src/main/java/com/papercut/dust/claim/AlreadyClaimedException.java
// public class AlreadyClaimedException extends RuntimeException {
// 
//     public AlreadyClaimedException(final Claim claim) {
//         this(claim.deviceName, claim.username);
//     }
// 
//     public AlreadyClaimedException(final String deviceName, final String username) {
//         super(username != null
//                 ? String.format("Device %s is already claimed by user %s", deviceName, username)
//                 : String.format("Device %s is already claimed", deviceName));
//     }
// 
// }
// 
// Path: src/main/java/com/papercut/dust/claim/NoSuchClaimException.java
// public class NoSuchClaimException extends RuntimeException {
// 
//     public NoSuchClaimException(final long deviceId, final long userId) {
//         super(String.format("There is no claim for device %s and user %s", deviceId, userId));
//     }
// 
// }
// 
// Path: src/main/java/com/papercut/dust/device/NoSuchUniqueDeviceException.java
// public class NoSuchUniqueDeviceException extends RuntimeException {
// 
//     public NoSuchUniqueDeviceException(final String message) {
//         super(message);
//     }
// 
//     public static NoSuchUniqueDeviceException none(final String keyword) {
//         return new NoSuchUniqueDeviceException("No unique matching device for '" + keyword + "'");
//     }
// 
//     public static NoSuchUniqueDeviceException tooMany(
//             final String keyword,
//             final String foundDeviceNames
//     ) {
//         return new NoSuchUniqueDeviceException("We found too many matching devices for '"
//                 + keyword + "': " + foundDeviceNames);
//     }
// }


import com.papercut.dust.claim.AlreadyClaimedException;
import com.papercut.dust.claim.NoSuchClaimException;
import com.papercut.dust.device.NoSuchUniqueDeviceException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
/*
 * Copyright (c) 2017 PaperCut Software International Pty. Ltd.
 *
 * https://www.papercut.com
 *
 * Use of this source code is governed by an MIT license.
 * See the project's LICENSE file for more information.
 */
package com.papercut.dust;



@Provider
public class CustomExceptionMapper implements ExceptionMapper<Exception> {

    @Override
    public Response toResponse(final Exception exception) {
        if (exception instanceof AlreadyClaimedException) {
            return withStatus(exception, Response.Status.CONFLICT);
        } else if (exception instanceof NoSuchClaimException) {
            return withStatus(exception, Response.Status.CONFLICT);
        } else if (exception instanceof NoSuchUniqueDeviceException) {
            return withStatus(exception, Response.Status.NOT_FOUND);
        } else {
// Path: src/main/java/com/papercut/dust/claim/AlreadyClaimedException.java
// public class AlreadyClaimedException extends RuntimeException {
// 
//     public AlreadyClaimedException(final Claim claim) {
//         this(claim.deviceName, claim.username);
//     }
// 
//     public AlreadyClaimedException(final String deviceName, final String username) {
//         super(username != null
//                 ? String.format("Device %s is already claimed by user %s", deviceName, username)
//                 : String.format("Device %s is already claimed", deviceName));
//     }
// 
// }
// 
// Path: src/main/java/com/papercut/dust/claim/NoSuchClaimException.java
// public class NoSuchClaimException extends RuntimeException {
// 
//     public NoSuchClaimException(final long deviceId, final long userId) {
//         super(String.format("There is no claim for device %s and user %s", deviceId, userId));
//     }
// 
// }
// 
// Path: src/main/java/com/papercut/dust/device/NoSuchUniqueDeviceException.java
// public class NoSuchUniqueDeviceException extends RuntimeException {
// 
//     public NoSuchUniqueDeviceException(final String message) {
//         super(message);
//     }
// 
//     public static NoSuchUniqueDeviceException none(final String keyword) {
//         return new NoSuchUniqueDeviceException("No unique matching device for '" + keyword + "'");
//     }
// 
//     public static NoSuchUniqueDeviceException tooMany(
//             final String keyword,
//             final String foundDeviceNames
//     ) {
//         return new NoSuchUniqueDeviceException("We found too many matching devices for '"
//                 + keyword + "': " + foundDeviceNames);
//     }
// }


// Path: src/main/java/com/papercut/dust/CustomExceptionMapper.java
import com.papercut.dust.claim.AlreadyClaimedException;
import com.papercut.dust.claim.NoSuchClaimException;
import com.papercut.dust.device.NoSuchUniqueDeviceException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

/*
 * Copyright (c) 2017 PaperCut Software International Pty. Ltd.
 *
 * https://www.papercut.com
 *
 * Use of this source code is governed by an MIT license.
 * See the project's LICENSE file for more information.
 */
package com.papercut.dust;



@Provider
public class CustomExceptionMapper implements ExceptionMapper<Exception> {

    @Override
    public Response toResponse(final Exception exception) {
        if (exception instanceof AlreadyClaimedException) {
            return withStatus(exception, Response.Status.CONFLICT);
        } else if (exception instanceof NoSuchClaimException) {
            return withStatus(exception, Response.Status.CONFLICT);
        } else if (exception instanceof NoSuchUniqueDeviceException) {
            return withStatus(exception, Response.Status.NOT_FOUND);
        } else {
            return withStatus(exception, Response.Status.INTERNAL_SERVER_ERROR);