Failure Handling in Go DRMAA (2012-11-11)

One thing not covered yet is the failure handling in Go DRMAA. The package documentation shows that an error is a pointer to an DRMAA Error type, which is a struct consisting of an error id (Id) and an human readable error message (Message). The type also fulfills the Go error interface (i.e. in Go terminology it's an “errorer”), which prints the error message, whenever the error.Error() method is called. That makes it simple to use in both relevant cases, when the program just has to log the error and when the program should react based on an expected error.

The error ids are derived from the DRMAA 1.0 IDL standard. The following list shows the Go DRMAA error ids:

  • "nil" in case of success
  • ErrorId
  • InternalError
  • DrmCommunicationFailure
  • AuthFailure
  • InvalidArgument
  • NoActiveSession
  • NoMemory
  • InvalidContactString
  • DefaultContactStringError
  • NoDefaultContactStringSelected
  • DrmsInitFailed
  • AlreadyActiveSession
  • DrmsExitError
  • InvalidAttributeFormat
  • InvalidAttributeValue
  • ConflictingAttributeValues
  • TryLater
  • DeniedByDrm
  • InvalidJob
  • ResumeInconsistentState
  • SuspendInconsistentState
  • HoldInconsistentState
  • ReleaseInconsistentState
  • ExitTimeout
  • NoRusage
  • NoMoreElements
  • NoErrno

The following program demonstrates the usage of error ids for control flow decisions. While there is just one session at a time allowed, a second session init() results in an error with the id AlreadyActiveSession. If such an error occurs during initialization, the session is usable (since it's already initialized) despite the error. In case an other error occurs during initialization, the program exits gracefully.

 

package main
 
import (
  "drmaa"
  "fmt"
  "os"
)
 
func main() {
  var session drmaa.Session
 
  session.Init("session=Init1")
  defer session.Exit()
 
  // ... 
 
  // opening a second session is illegal
  err := session.Init("session=Init2")
 
  // in case of an error check the error id for failure handling
  if err != nil {
    switch err.Id {
    case drmaa.AlreadyActiveSession:
      fmt.Println("We have already a session: ", err.Message)
      // let's continue by using the open session
    case drmaa.DrmsInitFailed:
      fmt.Println("Init failed: ", err.Message)
      os.Exit(1)
    default:
      fmt.Println("Unexpected error: ", err)
      os.Exit(2)
    }
  }
 
  contact, _ := drmaa.GetContact()
  fmt.Println("Session name: ", contact)
}
 

When running the program produces following output:

 

We have already a session: Initialization failed due to existing DRMAA session.
Session name: session=Init1