Error.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/DeprecatedFlyString.h>
  9. #include <AK/String.h>
  10. #include <LibJS/Runtime/Completion.h>
  11. #include <LibJS/Runtime/Object.h>
  12. #include <LibJS/SourceRange.h>
  13. namespace JS {
  14. struct TracebackFrame {
  15. DeprecatedFlyString function_name;
  16. [[nodiscard]] SourceRange const& source_range() const;
  17. mutable Variant<SourceRange, UnrealizedSourceRange> source_range_storage;
  18. };
  19. class Error : public Object {
  20. JS_OBJECT(Error, Object);
  21. public:
  22. static NonnullGCPtr<Error> create(Realm&);
  23. static NonnullGCPtr<Error> create(Realm&, String message);
  24. static NonnullGCPtr<Error> create(Realm&, StringView message);
  25. virtual ~Error() override = default;
  26. [[nodiscard]] String stack_string() const;
  27. ThrowCompletionOr<void> install_error_cause(Value options);
  28. Vector<TracebackFrame, 32> const& traceback() const { return m_traceback; }
  29. protected:
  30. explicit Error(Object& prototype);
  31. private:
  32. void populate_stack();
  33. Vector<TracebackFrame, 32> m_traceback;
  34. };
  35. // NOTE: Making these inherit from Error is not required by the spec but
  36. // our way of implementing the [[ErrorData]] internal slot, which is
  37. // used in Object.prototype.toString().
  38. #define DECLARE_NATIVE_ERROR(ClassName, snake_name, PrototypeName, ConstructorName) \
  39. class ClassName final : public Error { \
  40. JS_OBJECT(ClassName, Error); \
  41. \
  42. public: \
  43. static NonnullGCPtr<ClassName> create(Realm&); \
  44. static NonnullGCPtr<ClassName> create(Realm&, String message); \
  45. static NonnullGCPtr<ClassName> create(Realm&, StringView message); \
  46. \
  47. explicit ClassName(Object& prototype); \
  48. virtual ~ClassName() override = default; \
  49. };
  50. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  51. DECLARE_NATIVE_ERROR(ClassName, snake_name, PrototypeName, ConstructorName)
  52. JS_ENUMERATE_NATIVE_ERRORS
  53. #undef __JS_ENUMERATE
  54. }