本文整理匯總了Golang中github.com/dropbox/godropbox/errors.New函數的典型用法代碼示例。如果您正苦於以下問題:Golang New函數的具體用法?Golang New怎麽用?Golang New使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了New函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: NewBlobFieldDescriptor
// This returns a field descriptor for FieldType_BLOB (i.e., Field_blob)
func NewBlobFieldDescriptor(nullable NullableColumn, metadata []byte) (
fd FieldDescriptor,
remaining []byte,
err error) {
if len(metadata) < 1 {
return nil, nil, errors.New("Metadata has too few bytes")
}
packedLen := LittleEndian.Uint8(metadata)
if packedLen > 4 {
return nil, nil, errors.New("Invalid packed length")
}
return &blobFieldDescriptor{
packedLengthFieldDescriptor: packedLengthFieldDescriptor{
baseFieldDescriptor: baseFieldDescriptor{
fieldType: mysql_proto.FieldType_BLOB,
isNullable: nullable,
},
packedLength: int(packedLen),
},
}, metadata[1:], nil
}
示例2: WLock
// Lock for writing, waiting up to 'timeout' for successful exclusive
// acquisition of the lock.
func (rw *BoundedRWLock) WLock(timeout time.Duration) (err error) {
deadline := time.After(timeout)
rw.control.Lock()
if rw.readers != 0 || rw.nextWriter != nil {
me := newWait(true)
if rw.nextWriter == nil {
rw.nextWriter = me
} else {
select {
case rw.waiters <- me:
default:
err = errors.New("Waiter capacity reached in WLock")
}
}
rw.control.Unlock()
if err != nil {
return
}
woken := me.WaitAtomic(deadline)
if !woken {
return errors.New("Waiter timeout")
}
rw.control.Lock()
if rw.readers != 0 {
panic("readers??")
}
} else {
rw.nextWriter = newWait(true)
}
rw.control.Unlock()
return
}
示例3: init
func (d *usecTemporalFieldDescriptor) init(
fieldType mysql_proto.FieldType_Type,
nullable NullableColumn,
fixedSize int,
metadata []byte) (
remaining []byte,
err error) {
d.fieldType = fieldType
d.isNullable = nullable
if len(metadata) < 1 {
return nil, errors.New("Metadata has too few bytes")
}
d.fixedSize = fixedSize
d.neededBytes = fixedSize
d.microSecondPrecision = uint8(metadata[0])
switch d.microSecondPrecision {
case 0:
// do nothing
case 1, 2:
d.neededBytes++
case 3, 4:
d.neededBytes += 2
case 5, 6:
d.neededBytes += 3
default:
return nil, errors.New("Invalid usec precision")
}
return metadata[1:], nil
}
示例4: RLock
// Wait for a read lock for up to 'timeout'.
//
// Error will be non-nil on timeout or when the wait list is at capacity.
func (rw *BoundedRWLock) RLock(timeout time.Duration) (err error) {
deadline := time.After(timeout)
rw.control.Lock()
if rw.nextWriter != nil {
me := newWait(false)
select {
case rw.waiters <- me:
default:
err = errors.New("Waiter capacity reached in RLock")
}
rw.control.Unlock()
if err != nil {
return
}
woken := me.WaitAtomic(deadline)
if !woken {
return errors.New("Waiter timeout")
}
rw.control.Lock()
}
rw.readers++
rw.control.Unlock()
return
}
示例5: Page
func Page(q PageQuery) (*PageResponse, error) {
if q.ID == 0 {
return nil, errors.New("a page id is required")
}
resp, err := http.Get(q.urlString())
if err != nil {
return nil, errors.Wrap(err, "could not get http response")
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, errors.Wrapf(errors.New(resp.Status), "StatusCode: %d; URL: %s", resp.StatusCode, q.urlString())
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "could not read http response body")
}
var response PageResponse
if err := json.Unmarshal(body, &response); err != nil {
return nil, errors.Wrap(err, "could not unmarshal http response")
}
return &response, nil
}
示例6: String
func (s *lockStatementImpl) String(database string) (sql string, err error) {
if !validIdentifierName(database) {
return "", errors.New("Invalid database name specified")
}
if len(s.locks) == 0 {
return "", errors.New("No locks added")
}
buf := new(bytes.Buffer)
buf.WriteString("LOCK TABLES ")
for idx, lock := range s.locks {
if lock.t == nil {
return "", errors.Newf("nil table. Generated sql: %s", buf.String())
}
if err = lock.t.SerializeSql(database, buf); err != nil {
return
}
if lock.w {
buf.WriteString(" WRITE")
} else {
buf.WriteString(" READ")
}
if idx != len(s.locks)-1 {
buf.WriteString(", ")
}
}
return buf.String(), nil
}
示例7: Request
// Compile launches the search and compiles a list of weather list.
func Request(q *Query) ([]*Record, error) {
if q.Start.IsZero() || q.End.IsZero() {
return nil, errors.New("StartDate and EndDate (time.Time) values are required to compile weather data.")
}
if len(q.Stations) == 0 {
return nil, errors.New("at least one weather station is required to provide weather response")
}
var list []*Record
for _, station := range q.Stations {
values, err := q.getAPIList(station)
if err != nil {
return nil, err
}
for _, v := range values {
list = q.addToList(list, v, station)
}
}
sort.Sort(weatherSorter{list})
return list, nil
}
示例8: sendCountRequest
func (c *RawClient) sendCountRequest(
code opCode,
key string,
delta uint64,
initValue uint64,
expiration uint32) CountResponse {
if !c.validState {
// An error has occurred in one of the previous requests. It's not
// safe to continue sending.
return NewCountErrorResponse(
key,
errors.New("Skipping due to previous error"))
}
if !isValidKeyString(key) {
return NewCountErrorResponse(
key,
errors.New("Invalid key"))
}
err := c.sendRequest(
code,
0,
[]byte(key),
nil,
delta,
initValue,
expiration)
if err != nil {
c.validState = false
return NewCountErrorResponse(key, err)
}
return nil
}
示例9: parseUpdatedDbNames
func (p *QueryEventParser) parseUpdatedDbNames(data []byte, q *QueryEvent) (
[]byte,
error) {
if len(data) == 0 {
return data, errors.New("Not enough data")
}
q.numUpdatedDbs = new(uint8)
data, err := readLittleEndian(data, q.numUpdatedDbs)
if err != nil {
return data, err
}
if *q.numUpdatedDbs < MaxDbsInEventMts {
q.updatedDbNames = make([][]byte, *q.numUpdatedDbs, *q.numUpdatedDbs)
for i := uint8(0); i < *q.numUpdatedDbs; i++ {
idx := bytes.IndexByte(data, byte(0))
if idx > -1 {
q.updatedDbNames[i] = data[:idx]
data = data[idx+1:]
} else {
return data, errors.New("Not enough data")
}
}
}
return data, nil
}
示例10: NextEvent
func (r *rawV4EventReader) NextEvent() (Event, error) {
if r.isClosed {
return nil, errors.New("Event reader is closed")
}
if r.nextEvent == nil { // new event
r.nextEvent = &RawV4Event{
sourceName: r.srcName,
sourcePosition: r.logPosition,
}
}
if r.nextEvent.data == nil { // still parsing the header
headerBytes, err := r.getHeaderBuffer().PeekAll()
if err != nil {
return nil, err
}
_, err = readLittleEndian(headerBytes, &r.nextEvent.header)
if err != nil {
return nil, err
}
bodySize := int(r.nextEvent.EventLength()) - sizeOfBasicV4EventHeader
if bodySize < 0 { // should never happen
return nil, errors.New("Invalid event size")
}
r.nextEvent.data = make(
[]byte,
r.nextEvent.EventLength(),
r.nextEvent.EventLength())
copied := copy(r.nextEvent.data, headerBytes)
if copied != sizeOfBasicV4EventHeader { // should never happen
panic("Failed to copy header")
}
r.bodyBuffer = bufio2.NewLookAheadBufferUsing(
r.src,
r.nextEvent.data[sizeOfBasicV4EventHeader:])
}
_, err := r.bodyBuffer.PeekAll()
if err != nil {
return nil, err
}
// consume the constructed event and clean the look ahead buffers
event := r.nextEvent
r.nextEvent = nil
r.headerBuffer = nil
r.bodyBuffer = nil
r.logPosition += int64(event.EventLength())
return event, nil
}
示例11: DeleteMulti
func (c *RawAsciiClient) DeleteMulti(keys []string) []MutateResponse {
responses := make([]MutateResponse, len(keys), len(keys))
c.mutex.Lock()
defer c.mutex.Unlock()
// NOTE: delete requests are pipelined.
for i, key := range keys {
if !isValidKeyString(key) {
responses[i] = NewMutateErrorResponse(
key,
errors.New("Invalid key"))
continue
}
err := c.writeStrings("delete ", key, "\r\n")
if err != nil {
responses[i] = NewMutateErrorResponse(key, err)
}
}
err := c.flushWriter()
if err != nil {
// The delete requests may or may not have successfully reached the
// memcached, just error out.
for i, key := range keys {
if responses[i] == nil {
responses[i] = NewMutateErrorResponse(key, err)
}
}
}
for i, key := range keys {
if responses[i] != nil {
continue
}
line, err := c.readLine()
if err != nil {
responses[i] = NewMutateErrorResponse(key, err)
continue
}
if line == "DELETED" {
responses[i] = NewMutateResponse(key, StatusNoError, 0, true)
} else if line == "NOT_FOUND" {
responses[i] = NewMutateResponse(key, StatusKeyNotFound, 0, true)
} else { // Unexpected error msg
responses[i] = NewMutateErrorResponse(key, errors.New(line))
}
}
_ = c.checkEmptyBuffers()
return responses
}
示例12: countRequest
func (c *RawAsciiClient) countRequest(
cmd string,
key string,
delta uint64,
initValue uint64,
expiration uint32) CountResponse {
if expiration != 0xffffffff {
return NewCountErrorResponse(
key,
errors.New(
"Ascii protocol does not support initial value / "+
"expiration. expiration must be set to 0xffffffff."))
}
if !isValidKeyString(key) {
return NewCountErrorResponse(
key,
errors.New("Invalid key"))
}
c.mutex.Lock()
defer c.mutex.Unlock()
err := c.writeStrings(
cmd, " ",
key, " ",
strconv.FormatUint(delta, 10), "\r\n")
if err != nil {
return NewCountErrorResponse(key, err)
}
err = c.flushWriter()
if err != nil {
return NewCountErrorResponse(key, err)
}
line, err := c.readLine()
if err != nil {
return NewCountErrorResponse(key, err)
}
_ = c.checkEmptyBuffers()
if line == "NOT_FOUND" {
return NewCountResponse(key, StatusKeyNotFound, 0)
}
val, err := strconv.ParseUint(line, 10, 64)
if err != nil {
return NewCountErrorResponse(key, err)
}
return NewCountResponse(key, StatusNoError, val)
}
示例13: checkExtensionTypes
// checkExtensionTypes checks that the given extension is valid for pb.
func checkExtensionTypes(pb extendableProto, extension *ExtensionDesc) error {
// Check the extended type.
if a, b := reflect.TypeOf(pb), reflect.TypeOf(extension.ExtendedType); a != b {
return errors.New("proto: bad extended type; " + b.String() + " does not extend " + a.String())
}
// Check the range.
if !isExtensionField(pb, extension.Field) {
return errors.New("proto: bad extension number; not in declared ranges")
}
return nil
}
示例14: Stat
func (c *RawAsciiClient) Stat(statsKey string) StatResponse {
shardEntries := make(map[int](map[string]string))
entries := make(map[string]string)
shardEntries[c.ShardId()] = entries
if statsKey != "" {
return NewStatErrorResponse(
errors.New("Ascii protocol does not support specific stats lookup"),
shardEntries)
}
var err error
c.mutex.Lock()
defer c.mutex.Unlock()
err = c.writeStrings("stats\r\n")
if err != nil {
return NewStatErrorResponse(err, shardEntries)
}
err = c.flushWriter()
if err != nil {
return NewStatErrorResponse(err, shardEntries)
}
for {
line, err := c.readLine()
if err != nil {
NewStatErrorResponse(err, shardEntries)
}
if line == "END" {
break
}
// line is of the form: STAT <key> <value>
slice := strings.SplitN(line, " ", 3)
if len(slice) != 3 || slice[0] != "STAT" {
// The channel is no longer in valid state since we're exiting
// stats mid stream.
c.validState = false
return NewStatErrorResponse(errors.New(line), shardEntries)
}
entries[slice[1]] = slice[2]
}
_ = c.checkEmptyBuffers()
return NewStatResponse(StatusNoError, shardEntries)
}
示例15: checkEmptyBuffers
func (c *RawAsciiClient) checkEmptyBuffers() error {
if c.writer.Buffered() != 0 {
c.validState = false
return errors.New("writer buffer not fully flushed")
}
if c.reader.Buffered() != 0 {
c.validState = false
return errors.New("reader buffer not fully drained")
}
return nil
}