ErrorView.swift 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import FirebaseVertexAI
  15. import SwiftUI
  16. struct ErrorView: View {
  17. var error: Error
  18. @State private var isDetailsSheetPresented = false
  19. var body: some View {
  20. HStack {
  21. Text("An error occurred.")
  22. Button(action: { isDetailsSheetPresented.toggle() }) {
  23. Image(systemName: "info.circle")
  24. }
  25. }
  26. .frame(maxWidth: .infinity, alignment: .center)
  27. .listRowSeparator(.hidden)
  28. .sheet(isPresented: $isDetailsSheetPresented) {
  29. ErrorDetailsView(error: error)
  30. }
  31. }
  32. }
  33. #Preview {
  34. NavigationView {
  35. let errorPromptBlocked = GenerateContentError.promptBlocked(
  36. response: GenerateContentResponse(candidates: [
  37. CandidateResponse(content: ModelContent(role: "model", [
  38. """
  39. A _hypothetical_ model response.
  40. Cillum ex aliqua amet aliquip labore amet eiusmod consectetur reprehenderit sit commodo.
  41. """,
  42. ]),
  43. safetyRatings: [
  44. SafetyRating(category: .dangerousContent, probability: .high),
  45. SafetyRating(category: .harassment, probability: .low),
  46. SafetyRating(category: .hateSpeech, probability: .low),
  47. SafetyRating(category: .sexuallyExplicit, probability: .low),
  48. ],
  49. finishReason: FinishReason.other,
  50. citationMetadata: nil),
  51. ],
  52. promptFeedback: nil)
  53. )
  54. List {
  55. MessageView(message: ChatMessage.samples[0])
  56. MessageView(message: ChatMessage.samples[1])
  57. ErrorView(error: errorPromptBlocked)
  58. }
  59. .listStyle(.plain)
  60. .navigationTitle("Chat sample")
  61. }
  62. }