Exception.h 1.8 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

X
Xu Peng 已提交
18 19
#pragma once

20 21
#include "utils/Error.h"

X
Xu Peng 已提交
22 23 24 25
#include <exception>
#include <string>

namespace zilliz {
J
jinhai 已提交
26
namespace milvus {
X
Xu Peng 已提交
27 28

class Exception : public std::exception {
G
groot 已提交
29 30
 public:
    Exception(ErrorCode code, const std::string &message)
G
groot 已提交
31
        : code_(code),
32
          message_(message) {
G
groot 已提交
33
    }
X
Xu Peng 已提交
34

35 36 37
    ErrorCode code() const throw() {
        return code_;
    }
X
Xu Peng 已提交
38

G
groot 已提交
39
    virtual const char *what() const throw() {
X
Xu Peng 已提交
40 41 42 43 44 45 46
        if (message_.empty()) {
            return "Default Exception.";
        } else {
            return message_.c_str();
        }
    }

G
groot 已提交
47 48
    virtual ~Exception() throw() {
    }
X
Xu Peng 已提交
49

G
groot 已提交
50
 protected:
51
    ErrorCode code_;
X
Xu Peng 已提交
52 53 54 55
    std::string message_;
};

class InvalidArgumentException : public Exception {
G
groot 已提交
56
 public:
57 58
    InvalidArgumentException()
        : Exception(SERVER_INVALID_ARGUMENT, "Invalid Argument") {
G
groot 已提交
59
    }
60

G
groot 已提交
61
    explicit InvalidArgumentException(const std::string &message)
62
        : Exception(SERVER_INVALID_ARGUMENT, message) {
G
groot 已提交
63
    }
X
Xu Peng 已提交
64 65
};

J
jinhai 已提交
66
} // namespace milvus
X
Xu Peng 已提交
67
} // namespace zilliz