001/*----------------------------------------------------------------------------*/
002/* Copyright (c) FIRST 2008-2017. All Rights Reserved.                        */
003/* Open Source Software - may be modified and shared by FRC teams. The code   */
004/* must be accompanied by the FIRST BSD license file in the root directory of */
005/* the project.                                                               */
006/*----------------------------------------------------------------------------*/
007
008package edu.wpi.first.wpilibj.can;
009
010import edu.wpi.first.wpilibj.communication.NIRioStatus;
011import edu.wpi.first.wpilibj.util.UncleanStatusException;
012
013public class CANExceptionFactory {
014  // FRC Error codes
015  static final int ERR_CANSessionMux_InvalidBuffer = -44086;
016  static final int ERR_CANSessionMux_MessageNotFound = -44087;
017  static final int ERR_CANSessionMux_NotAllowed = -44088;
018  static final int ERR_CANSessionMux_NotInitialized = -44089;
019
020  @SuppressWarnings("JavadocMethod")
021  public static void checkStatus(int status, int messageID) throws CANInvalidBufferException,
022      CANMessageNotAllowedException, CANNotInitializedException, UncleanStatusException {
023    switch (status) {
024      case NIRioStatus.kRioStatusSuccess:
025        // Everything is ok... don't throw.
026        return;
027      case ERR_CANSessionMux_InvalidBuffer:
028      case NIRioStatus.kRIOStatusBufferInvalidSize:
029        throw new CANInvalidBufferException();
030      case ERR_CANSessionMux_MessageNotFound:
031      case NIRioStatus.kRIOStatusOperationTimedOut:
032        throw new CANMessageNotFoundException();
033      case ERR_CANSessionMux_NotAllowed:
034      case NIRioStatus.kRIOStatusFeatureNotSupported:
035        throw new CANMessageNotAllowedException("MessageID = " + Integer.toString(messageID));
036      case ERR_CANSessionMux_NotInitialized:
037      case NIRioStatus.kRIOStatusResourceNotInitialized:
038        throw new CANNotInitializedException();
039      default:
040        throw new UncleanStatusException("Fatal status code detected:  " + Integer.toString(
041            status));
042    }
043  }
044}