|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.pytorch.executorch; |
| 10 | + |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +public class ExecutorchRuntimeException extends RuntimeException { |
| 16 | + // Error code constants - keep in sync with runtime/core/error.h |
| 17 | + // System errors |
| 18 | + public static final int OK = 0x00; |
| 19 | + public static final int INTERNAL = 0x01; |
| 20 | + public static final int INVALID_STATE = 0x02; |
| 21 | + public static final int END_OF_METHOD = 0x03; |
| 22 | + |
| 23 | + // Logical errors |
| 24 | + public static final int NOT_SUPPORTED = 0x10; |
| 25 | + public static final int NOT_IMPLEMENTED = 0x11; |
| 26 | + public static final int INVALID_ARGUMENT = 0x12; |
| 27 | + public static final int INVALID_TYPE = 0x13; |
| 28 | + public static final int OPERATOR_MISSING = 0x14; |
| 29 | + public static final int REGISTRATION_EXCEEDING_MAX_KERNELS = 0x15; |
| 30 | + public static final int REGISTRATION_ALREADY_REGISTERED = 0x16; |
| 31 | + |
| 32 | + // Resource errors |
| 33 | + public static final int NOT_FOUND = 0x20; |
| 34 | + public static final int MEMORY_ALLOCATION_FAILED = 0x21; |
| 35 | + public static final int ACCESS_FAILED = 0x22; |
| 36 | + public static final int INVALID_PROGRAM = 0x23; |
| 37 | + public static final int INVALID_EXTERNAL_DATA = 0x24; |
| 38 | + public static final int OUT_OF_RESOURCES = 0x25; |
| 39 | + |
| 40 | + // Delegate errors |
| 41 | + public static final int DELEGATE_INVALID_COMPATIBILITY = 0x30; |
| 42 | + public static final int DELEGATE_MEMORY_ALLOCATION_FAILED = 0x31; |
| 43 | + public static final int DELEGATE_INVALID_HANDLE = 0x32; |
| 44 | + |
| 45 | + private static final Map<Integer, String> ERROR_CODE_MESSAGES; |
| 46 | + |
| 47 | + static { |
| 48 | + Map<Integer, String> map = new HashMap<>(); |
| 49 | + |
| 50 | + // System errors |
| 51 | + map.put(OK, "Operation successful"); |
| 52 | + map.put(INTERNAL, "Internal error"); |
| 53 | + map.put(INVALID_STATE, "Invalid state"); |
| 54 | + map.put(END_OF_METHOD, "End of method reached"); |
| 55 | + // Logical errors |
| 56 | + map.put(NOT_SUPPORTED, "Operation not supported"); |
| 57 | + map.put(NOT_IMPLEMENTED, "Operation not implemented"); |
| 58 | + map.put(INVALID_ARGUMENT, "Invalid argument"); |
| 59 | + map.put(INVALID_TYPE, "Invalid type"); |
| 60 | + map.put(OPERATOR_MISSING, "Operator missing"); |
| 61 | + map.put(REGISTRATION_EXCEEDING_MAX_KERNELS, "Exceeded max kernels"); |
| 62 | + map.put(REGISTRATION_ALREADY_REGISTERED, "Kernel already registered"); |
| 63 | + // Resource errors |
| 64 | + map.put(NOT_FOUND, "Resource not found"); |
| 65 | + map.put(MEMORY_ALLOCATION_FAILED, "Memory allocation failed"); |
| 66 | + map.put(ACCESS_FAILED, "Access failed"); |
| 67 | + map.put(INVALID_PROGRAM, "Invalid program"); |
| 68 | + map.put(INVALID_EXTERNAL_DATA, "Invalid external data"); |
| 69 | + map.put(OUT_OF_RESOURCES, "Out of resources"); |
| 70 | + // Delegate errors |
| 71 | + map.put(DELEGATE_INVALID_COMPATIBILITY, "Delegate invalid compatibility"); |
| 72 | + map.put(DELEGATE_MEMORY_ALLOCATION_FAILED, "Delegate memory allocation failed"); |
| 73 | + map.put(DELEGATE_INVALID_HANDLE, "Delegate invalid handle"); |
| 74 | + ERROR_CODE_MESSAGES = Collections.unmodifiableMap(map); |
| 75 | + } |
| 76 | + |
| 77 | + static class ErrorHelper { |
| 78 | + static String formatMessage(int errorCode, String details) { |
| 79 | + String baseMessage = ERROR_CODE_MESSAGES.get(errorCode); |
| 80 | + if (baseMessage == null) { |
| 81 | + baseMessage = "Unknown error code 0x" + Integer.toHexString(errorCode); |
| 82 | + } |
| 83 | + return "[Executorch Error 0x" |
| 84 | + + Integer.toHexString(errorCode) |
| 85 | + + "] " |
| 86 | + + baseMessage |
| 87 | + + ": " |
| 88 | + + details; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private final int errorCode; |
| 93 | + |
| 94 | + public ExecutorchRuntimeException(int errorCode, String details) { |
| 95 | + super(ErrorHelper.formatMessage(errorCode, details)); |
| 96 | + this.errorCode = errorCode; |
| 97 | + } |
| 98 | + |
| 99 | + public int getErrorCode() { |
| 100 | + return errorCode; |
| 101 | + } |
| 102 | + |
| 103 | + // Idiomatic Java exception for invalid arguments. |
| 104 | + public static class ExecutorchInvalidArgumentException extends IllegalArgumentException { |
| 105 | + private final int errorCode = INVALID_ARGUMENT; |
| 106 | + |
| 107 | + public ExecutorchInvalidArgumentException(String details) { |
| 108 | + super(ErrorHelper.formatMessage(INVALID_ARGUMENT, details)); |
| 109 | + } |
| 110 | + |
| 111 | + public int getErrorCode() { |
| 112 | + return errorCode; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // Factory method to create an exception of the appropriate subclass. |
| 117 | + public static RuntimeException makeExecutorchException(int errorCode, String details) { |
| 118 | + switch (errorCode) { |
| 119 | + case INVALID_ARGUMENT: |
| 120 | + return new ExecutorchInvalidArgumentException(details); |
| 121 | + default: |
| 122 | + return new ExecutorchRuntimeException(errorCode, details); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments