errors.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 optdec
  17. import (
  18. "encoding/json"
  19. "errors"
  20. "reflect"
  21. "strconv"
  22. "github.com/bytedance/sonic/internal/rt"
  23. )
  24. /** JIT Error Helpers **/
  25. var stackOverflow = &json.UnsupportedValueError{
  26. Str: "Value nesting too deep",
  27. Value: reflect.ValueOf("..."),
  28. }
  29. func error_type(vt *rt.GoType) error {
  30. return &json.UnmarshalTypeError{Type: vt.Pack()}
  31. }
  32. func error_mismatch(node Node, ctx *context, typ reflect.Type) error {
  33. return MismatchTypeError{
  34. Pos: node.Position(),
  35. Src: ctx.Parser.Json,
  36. Type: typ,
  37. }
  38. }
  39. func newUnmatched(pos int, vt *rt.GoType) error {
  40. return MismatchTypeError{
  41. Pos: pos,
  42. Src: "",
  43. Type: vt.Pack(),
  44. }
  45. }
  46. func error_field(name string) error {
  47. return errors.New("json: unknown field " + strconv.Quote(name))
  48. }
  49. func error_value(value string, vtype reflect.Type) error {
  50. return &json.UnmarshalTypeError{
  51. Type: vtype,
  52. Value: value,
  53. }
  54. }
  55. func error_syntax(pos int, src string, msg string) error {
  56. return SyntaxError{
  57. Pos: pos,
  58. Src: src,
  59. Msg: msg,
  60. }
  61. }
  62. func error_unsuppoted(typ *rt.GoType) error {
  63. return &json.UnsupportedTypeError{
  64. Type: typ.Pack(),
  65. }
  66. }