errors.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright 2021 ByteDance Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package vars
  17. import (
  18. `encoding/json`
  19. `fmt`
  20. `reflect`
  21. `strconv`
  22. `unsafe`
  23. `github.com/bytedance/sonic/internal/rt`
  24. )
  25. var ERR_too_deep = &json.UnsupportedValueError {
  26. Str : "Value nesting too deep",
  27. Value : reflect.ValueOf("..."),
  28. }
  29. var ERR_nan_or_infinite = &json.UnsupportedValueError {
  30. Str : "NaN or ±Infinite",
  31. Value : reflect.ValueOf("NaN or ±Infinite"),
  32. }
  33. func Error_type(vtype reflect.Type) error {
  34. return &json.UnsupportedTypeError{Type: vtype}
  35. }
  36. func Error_number(number json.Number) error {
  37. return &json.UnsupportedValueError {
  38. Str : "invalid number literal: " + strconv.Quote(string(number)),
  39. Value : reflect.ValueOf(number),
  40. }
  41. }
  42. func Error_unsuppoted(typ *rt.GoType) error {
  43. return &json.UnsupportedTypeError{Type: typ.Pack() }
  44. }
  45. func Error_marshaler(ret []byte, pos int) error {
  46. return fmt.Errorf("invalid Marshaler output json syntax at %d: %q", pos, ret)
  47. }
  48. const (
  49. PanicNilPointerOfNonEmptyString int = 1 + iota
  50. )
  51. func GoPanic(code int, val unsafe.Pointer) {
  52. switch(code){
  53. case PanicNilPointerOfNonEmptyString:
  54. panic(fmt.Sprintf("val: %#v has nil pointer while its length is not zero!\nThis is a nil pointer exception (NPE) problem. There might be a data race issue. It is recommended to execute the tests related to the code with the `-race` compile flag to detect the problem.", (*rt.GoString)(val)))
  55. default:
  56. panic("encoder error!")
  57. }
  58. }