P4Message struct

Description

The P4Message struct provides a utility for managing and accessing messages generated by the P4 Server . These messages are typically returned as part of command execution and can include information, warnings, errors, or fatal messages. The P4Message struct encapsulates the severity of the message, its unique code, and the formatted message text, making it easier to handle and interpret server responses.

Severity of the message, which is one of the following values:

Value Meaning
E_EMPTY

No error

E_INFO

Informational message only

E_WARN

Warning message only

E_FAILED

Command failed

E_FATAL

Severe error; cannot continue.

Struct Fields

severity P4MessageSeverity

Represents the severity of the message. Possible values include info, warn, failed, fatal, or empty.

lines []P4MessageLine

A slice of P4MessageLine structs, where each line contains details about a specific part of the message, such as its severity, code, and formatted text.

Table of Methods

P4Message is a utility struct that gets attributes of message objects returned by P4 commands.

Method Type Description

ArgCount(i int)

Int

Returns the number of arguments in the message line at the specified index.

Count()

Int

Returns the number of lines in the message.

Error()

String

Returns the error message as a string.

Generic(i int)

Int

Returns the generic error code of the message line at the specified index.

GetLine(i int)

P4MessageLine

Returns the message line object at the specified index.

GetMsgDict()

map[string]string

Returns the message dictionary containing additional details.

Id(i int)

Int

Returns the unique ID of the message line at the specified index.

LineSeverity(i int)

P4MessageSeverity

Returns the severity of the message line at the specified index.

ResultType()

P4ResultType

Returns the result type of the message.

Severity()

P4MessageSeverity

Returns the severity level of the message.

String()

String

Returns the string representation of the message.

SubCode(i int)

Int

Returns the subcode of the message line at the specified index.

Subsystem(i int)

Int

Returns the subsystem code of the message line at the specified index.

UniqueCode(i int)

Int

Returns the unique code of the message line at the specified index.

Example

This example demonstrates how to handle and extract detailed information from a P4Message when a command fails.

Copy
package main

import (
    "fmt"
    "p4"
)

func main() {
    // Initialize the Perforce client
    p4api := p4.New()
    defer p4api.Close()
    // Connect to the P4 server
    connected, err := p4api.Connect()
    if err != nil || !connected {
        fmt.Println("Failed to connect to P4 server:", err)
        return
    }
    defer p4api.Disconnect()
    client_specs, _ := p4api.RunFetch("client", "mytest")
    client_specs["Root"] = "path/to/client/root"
    p4api.RunSave("client", client_specs)
    p4api.SetClient("mytest")
    
    // Run a Perforce command that is expected to fail
    // For example, trying to sync a non-existent file
    res, err := p4api.Run("sync", "//nonexistent/file/...")
    if err != nil {
        fmt.Println("Failed to sync files:", err)
        return
    }

    // Check if the result contains a P4Message
    for _, item := range res {
        if msg, ok := item.(P4Message); ok {
            // Demonstrate all P4Message methods
            fmt.Println("P4Message Details:")
            fmt.Println("Severity:", msg.Severity())
            fmt.Println("Count:", msg.Count())

            for i := 0; i < msg.Count(); i++ {
                fmt.Printf("\nMessage Line %d:\n", i+1)
                fmt.Println("  Line Severity:", msg.LineSeverity(i))
                fmt.Println("  Unique Code:", msg.UniqueCode(i))
                fmt.Println("  SubCode:", msg.SubCode(i))
                fmt.Println("  Subsystem:", msg.Subsystem(i))
                fmt.Println("  Generic Code:", msg.Generic(i))
                fmt.Println("  Argument Count:", msg.ArgCount(i))
                fmt.Println("  Line Content:", msg.GetLine(i))
                fmt.Println("  Line Data:", msg.Id(i))
                fmt.Println("  ResultType:", msg.ResultType())
            }

            // Print the full message as a string
            fmt.Println("\nFull Message:")
            fmt.Println(msg.String())

            // Use the Error() method to get the error string
            fmt.Println("\nError String:")
            fmt.Println(msg.Error())

            // Print the message dictionary
            fmt.Println("\nMessage Dictionary:")
            fmt.Println(msg.GetMsgDict())
        } else {
            fmt.Println("Item is not a P4Message")
        }
    }
    p4api.Disconnect()
}

Output:

Copy
P4Message Details:
            Severity: failed
            Count: 1
            Message Line 1:
            Line Severity: failed
            Unique Code: 6244
            SubCode: 100
            Subsystem: 6
            Generic Code: 2
            Argument Count: 2
            Line Content: {3 838998116 //nonexistent/file/... - must refer to client 'mytest' or a depot.}
            Line Data: 838998116
            ResultType: 4
            Full Message:
            //nonexistent/file/... - must refer to client 'mytest' or a depot.
            Error String:
            //nonexistent/file/... - must refer to client 'mytest' or a depot.
            Message Dictionary:
            map[Error 0://nonexistent/file/... - must refer to client 'mytest' or a depot. client:mytest code0:838998116 fmt0:%path% - must refer to client '%client%' or a depot. func:client-Message path://nonexistent/file/...]