p4error.cpp #4

  • //
  • guest/
  • perforce_software/
  • p4ruby/
  • main/
  • p4error.cpp
  • View
  • Commits
  • Open Download .zip Download (3 KB)
/*******************************************************************************

Copyright (c) 2010, Perforce Software, Inc.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1.  Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.

2.  Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*******************************************************************************/

/*******************************************************************************
 * Name		: p4error.cc
 *
 * Author	: Tony Smith <[email protected]> or <[email protected]>
 *
 * Description	: Class for bridging Perforce's Error class to Ruby
 *
 ******************************************************************************/
#include <ruby.h>
#include "undefdups.h"
#include <clientapi.h>
#include "extconf.h"
#include "p4rubydebug.h"
#include "p4utils.h"
#include "p4error.h"


static void error_free( P4Error *e )
{
    delete e;
}

static void error_mark( P4Error *e )
{
    e->GCMark();
}


P4Error::P4Error( const Error &other )
{
    this->debug = 0;

    error = other;
}

VALUE
P4Error::GetId()
{
    ErrorId *id = error.GetId( 0 );
    if( !id )
	return INT2NUM( 0 );
    return INT2NUM( id->UniqueCode() );
}

VALUE
P4Error::GetGeneric()
{
    return INT2NUM( error.GetGeneric() );
}

VALUE
P4Error::GetSeverity()
{
    return INT2NUM( error.GetSeverity() );
}

VALUE
P4Error::GetText()
{
    StrBuf t;
    error.Fmt( t, EF_PLAIN );
    return P4Utils::ruby_string( t.Text(), t.Length() );
}

VALUE
P4Error::Inspect()
{
    StrBuf a;
    StrBuf b;

    error.Fmt( a, EF_PLAIN );
    b << "[";
    b << "Gen:" << error.GetGeneric();
    b << "/Sev:" << error.GetSeverity();
    b << "]: ";
    b << a;
    return P4Utils::ruby_string( b.Text(), b.Length() );
}

VALUE
P4Error::Wrap( VALUE pClass )
{
    VALUE e;
    VALUE argv[ 1 ];

    e = Data_Wrap_Struct( pClass, error_mark, error_free, this );
    rb_obj_call_init( e, 0, argv );
    return e;
}

void
P4Error::GCMark()
{
    // We don't hold Ruby objects
}

# Change User Description Committed
#5 14682 Git Fusion Git Fusion branch management

Imported from Git
 ghost-of-change-num: 960958
 ghost-of-sha1: 005052ae424bd69f426f7209e741ca1c8c3253c7
 ghost-precedes-sha1: ad052c71a568ef12165e143a6866ad9ceffbb4a1
 parent-branch: None@960958
 push-state: incomplete
#4 14608 jmistry Add encoding to Strings

As part of adding Ruby 1.9 support we need to associate the encoding
for Ruby's strings from the server.  This approach is similar to Sven's
(in changelist 257263), where everything but the 'content' charset was
set to 'utf8'.  The content charset is picked up from P4CHARSET and this
is used to translate any file content.

Also disabled the Ruby 1.9 warning for each compile.

User visible change to be documented in release notes.
#3 14581 Sven Erik Knop Fixed error handling in P4Ruby.
The error message string did not get properly set in the exception and P4Message
object. This is because theP4API Error class does not have a proper copy
constructor. Since it has a decent assignment operator, I gave P4Error an Error
field and removed the superclass, which solved the problem and also made the
code more readable.
I also moved the Reset of the Result object in P4ClientAPI before the connection
check, so that 'not connected' can be shown in the exception instead of the
previous exception in case the connection is lost.

Bug fix to previously unreleased behaviour.
#2 14580 tony Added P4::Message#msgid method that returns the UniqueCode
of any error message - much easier than matching it's output
text.

User-visible enhancement documented in p4rubynotes.txt
#1 14579 tony Make new class P4::Message for returning Error objects
to the user. Currently handles errors and warnings, but
could potentially be used for output too (might bloat
people's code though).

Essentially, if you're using a 2010.2 or later client,
or if you've set your api_level to 68 or higher, the
P4#errors and P4#warnings arrays will be populated
with P4::Message objects instead of strings. Users of
older API's, or those who set their api_level to 67
or lower in their scripts will get the old behaviour.

P4::Message objects have the following methods:

  severity() - returns the severity
  generic()  - returns the generic code
  to_s()     - converts the message to a string
  inspect()  - returns a string showing the message details.

User-visible enhancement documented in p4rubynotes.txt