/** * Require that a non-String field be non-null. * * @param field * @throws SiteWhereException */ protected static void requireNotNull(Object field) throws SiteWhereException { if (field == null) { throw new SiteWhereSystemException(ErrorCode.IncompleteData, ErrorLevel.ERROR); } }
/** * Check whether an HTTP response code was set. * * @return */ public boolean hasHttpResponseCode() { return getHttpResponseCode() != -1; } }
public SiteWhereSystemException(ErrorCode code, ErrorLevel level, int httpResponseCode) { this(code, null, level); setHttpResponseCode(httpResponseCode); }
if (e instanceof SiteWhereSystemException) { SiteWhereSystemException sse = (SiteWhereSystemException) e; String combined = sse.getCode() + ":" + e.getMessage(); response.setHeader(ISiteWhereWebConstants.HEADER_SITEWHERE_ERROR, e.getMessage()); response.setHeader(ISiteWhereWebConstants.HEADER_SITEWHERE_ERROR_CODE, String.valueOf(sse.getCode())); if (sse.hasHttpResponseCode()) { response.sendError(sse.getHttpResponseCode(), combined); } else { response.sendError(HttpServletResponse.SC_BAD_REQUEST, combined);
svtResponse.setStatus(HttpServletResponse.SC_CREATED); } catch (SiteWhereSystemException e) { if (e.getCode() == ErrorCode.InvalidStreamId) { svtResponse.setStatus(HttpServletResponse.SC_NOT_FOUND); } else {
/** * Convert server exception to one that can be passed back via GRPC. * * @param t * @return */ public static StatusException convertServerException(Throwable t) { StatusException thrown = null; if (t instanceof SiteWhereSystemException) { SiteWhereSystemException sysex = (SiteWhereSystemException) t; Status status = Status.fromCode(Code.FAILED_PRECONDITION) .withDescription(sysex.getCode().getCode() + ":" + sysex.getCode().getMessage()); thrown = status.asException(); } else if (t instanceof SiteWhereException) { SiteWhereException sw = (SiteWhereException) t; Status status = Status.fromCode(Code.FAILED_PRECONDITION) .withDescription(ErrorCode.Error.getCode() + ":" + sw.getMessage()); thrown = status.asException(); } else if (t instanceof TenantEngineNotAvailableException) { TenantEngineNotAvailableException sw = (TenantEngineNotAvailableException) t; Status status = Status.fromCode(Code.UNAVAILABLE).withDescription(sw.getMessage()); thrown = status.asException(); } else { thrown = Status.fromThrowable(t).asException(); } return thrown; } }
@Override public void throwInvalidKey() throws SiteWhereException { throw new SiteWhereSystemException(ErrorCode.InvalidTenantId, ErrorLevel.ERROR); } };
/** * Check whether an HTTP response code was set. * * @return */ public boolean hasHttpResponseCode() { return getHttpResponseCode() != -1; } }
public SiteWhereSystemException(ErrorCode code, ErrorLevel level, int httpResponseCode) { this(code, null, level); setHttpResponseCode(httpResponseCode); }
@Override public void throwInvalidKey() throws SiteWhereException { throw new SiteWhereSystemException(ErrorCode.InvalidDeviceGroupToken, ErrorLevel.ERROR); } };
@Override public void throwInvalidKey() throws SiteWhereException { throw new SiteWhereSystemException(ErrorCode.InvalidBatchOperationToken, ErrorLevel.ERROR); } };
@Override public void throwInvalidKey() throws SiteWhereException { throw new SiteWhereSystemException(ErrorCode.InvalidDeviceSpecificationToken, ErrorLevel.ERROR); } };
@Override public void throwInvalidKey() throws SiteWhereException { throw new SiteWhereSystemException(ErrorCode.InvalidScheduleToken, ErrorLevel.ERROR); } };
@Override public void throwInvalidKey() throws SiteWhereException { throw new SiteWhereSystemException(ErrorCode.InvalidScheduledJobToken, ErrorLevel.ERROR); } };
@Override public void throwInvalidKey() throws SiteWhereException { throw new SiteWhereSystemException(ErrorCode.InvalidAssetCategoryId, ErrorLevel.ERROR); } };
/** * Requires that a String field be a non null, non space-filled value. * * @param field * @throws SiteWhereException */ protected static void require(String field) throws SiteWhereException { if (StringUtils.isBlank(field)) { throw new SiteWhereSystemException(ErrorCode.IncompleteData, ErrorLevel.ERROR); } }
public void addOrReplaceMetadata(String name, String value) throws SiteWhereException { if (!name.matches("^[\\w-]+$")) { throw new SiteWhereSystemException(ErrorCode.InvalidMetadataFieldName, ErrorLevel.ERROR); } entries.put(name, value); }
/** * Logic applied before deleting a device assignment. * * @param assignment * @throws SiteWhereException */ public static void deviceAssignmentDeleteLogic(IDeviceAssignment assignment) throws SiteWhereException { if (assignment.getReleasedDate() == null) { throw new SiteWhereSystemException(ErrorCode.CanNotDeleteActiveAssignment, ErrorLevel.ERROR); } }
@Override public boolean canInitialize() throws SiteWhereException { if (getLifecycleStatus() == LifecycleStatus.Started) { throw new SiteWhereSystemException(ErrorCode.TenantAlreadyStarted, ErrorLevel.ERROR); } return true; }
@Override public boolean canStart() throws SiteWhereException { if (getLifecycleStatus() == LifecycleStatus.Started) { throw new SiteWhereSystemException(ErrorCode.TenantAlreadyStarted, ErrorLevel.ERROR); } return true; }