001/*
002 * Copyright 2015-2020 the original author or authors
003 *
004 * This software is licensed under the Apache License, Version 2.0,
005 * the GNU Lesser General Public License version 2 or later ("LGPL")
006 * and the WTFPL.
007 * You may choose either license to govern your use of this software only
008 * upon the condition that you accept all of the terms of either
009 * the Apache License 2.0, the LGPL 2.1+ or the WTFPL.
010 */
011package org.minidns;
012
013import java.io.IOException;
014
015import org.minidns.dnsmessage.DnsMessage;
016import org.minidns.dnsqueryresult.DnsQueryResult;
017
018public abstract class MiniDnsException extends IOException {
019    /**
020     * 
021     */
022    private static final long serialVersionUID = 1L;
023
024    protected MiniDnsException(String message) {
025        super(message);
026    }
027
028    public static class IdMismatch extends MiniDnsException {
029
030        /**
031         * 
032         */
033        private static final long serialVersionUID = 1L;
034
035        private final DnsMessage request;
036        private final DnsMessage response;
037
038        public IdMismatch(DnsMessage request, DnsMessage response) {
039            super(getString(request, response));
040            assert request.id != response.id;
041            this.request = request;
042            this.response = response;
043        }
044
045        public DnsMessage getRequest() {
046            return request;
047        }
048
049        public DnsMessage getResponse() {
050            return response;
051        }
052
053        private static String getString(DnsMessage request, DnsMessage response) {
054            return "The response's ID doesn't matches the request ID. Request: " + request.id + ". Response: " + response.id;
055        }
056    }
057
058    public static class NullResultException extends MiniDnsException {
059
060        /**
061         * 
062         */
063        private static final long serialVersionUID = 1L;
064
065        private final DnsMessage request;
066
067        public NullResultException(DnsMessage request) {
068            super("The request yielded a 'null' result while resolving.");
069            this.request = request;
070        }
071
072        public DnsMessage getRequest() {
073            return request;
074        }
075    }
076
077    public static class ErrorResponseException extends MiniDnsException {
078
079        /**
080         *
081         */
082        private static final long serialVersionUID = 1L;
083
084        private final DnsMessage request;
085        private final DnsQueryResult result;
086
087        public ErrorResponseException(DnsMessage request, DnsQueryResult result) {
088            super("Received " + result.response.responseCode + " error response\n" + result);
089            this.request = request;
090            this.result = result;
091        }
092
093        public DnsMessage getRequest() {
094            return request;
095        }
096
097        public DnsQueryResult getResult() {
098            return result;
099        }
100    }
101
102    public static class NoQueryPossibleException extends MiniDnsException {
103
104        /**
105         *
106         */
107        private static final long serialVersionUID = 1L;
108
109        private final DnsMessage request;
110
111        public NoQueryPossibleException(DnsMessage request) {
112            super("No DNS server could be queried");
113            this.request = request;
114        }
115
116        public DnsMessage getRequest() {
117            return request;
118        }
119    }
120}