001package com.box.sdkgen.box.errors; 002 003import com.box.sdkgen.internal.logging.DataSanitizer; 004import com.box.sdkgen.networking.fetchresponse.FetchResponse; 005import com.fasterxml.jackson.databind.JsonNode; 006import java.time.LocalDateTime; 007import java.util.Optional; 008import okhttp3.Request; 009 010public class BoxAPIError extends BoxSDKError { 011 012 public final RequestInfo requestInfo; 013 014 public final ResponseInfo responseInfo; 015 016 private final DataSanitizer dataSanitizer; 017 018 public BoxAPIError(String message, RequestInfo requestInfo, ResponseInfo responseInfo) { 019 super(message); 020 this.requestInfo = requestInfo; 021 this.responseInfo = responseInfo; 022 this.dataSanitizer = new DataSanitizer(); 023 } 024 025 public static BoxAPIError fromAPICall( 026 Request request, 027 FetchResponse fetchResponse, 028 String rawResponseBody, 029 DataSanitizer dataSanitizer) { 030 return fromAPICall(request, fetchResponse, rawResponseBody, dataSanitizer, null); 031 } 032 033 public static BoxAPIError fromAPICall( 034 Request request, 035 FetchResponse fetchResponse, 036 String rawResponseBody, 037 DataSanitizer dataSanitizer, 038 String contentType) { 039 RequestInfo requestInfo = RequestInfo.fromRequest(request, contentType); 040 ResponseInfo responseInfo = ResponseInfo.fromResponse(fetchResponse, rawResponseBody); 041 042 String requestId = 043 Optional.ofNullable(responseInfo.getBody()) 044 .map(body -> body.get("request_id")) 045 .map(JsonNode::asText) 046 .orElse(""); 047 048 return new Builder( 049 String.format("Status %d; Request ID: %s", responseInfo.getStatusCode(), requestId), 050 requestInfo, 051 responseInfo) 052 .timestamp(LocalDateTime.now().toString()) 053 .dataSanitizer(dataSanitizer) 054 .build(); 055 } 056 057 protected BoxAPIError(Builder builder) { 058 super(builder); 059 this.requestInfo = builder.requestInfo; 060 this.responseInfo = builder.responseInfo; 061 this.dataSanitizer = builder.dataSanitizer; 062 } 063 064 public RequestInfo getRequestInfo() { 065 return requestInfo; 066 } 067 068 public ResponseInfo getResponseInfo() { 069 return responseInfo; 070 } 071 072 public static class Builder extends BoxSDKError.Builder { 073 074 protected final RequestInfo requestInfo; 075 076 protected final ResponseInfo responseInfo; 077 078 protected DataSanitizer dataSanitizer; 079 080 public Builder(String message, RequestInfo requestInfo, ResponseInfo responseInfo) { 081 super(message); 082 this.requestInfo = requestInfo; 083 this.responseInfo = responseInfo; 084 this.dataSanitizer = new DataSanitizer(); 085 } 086 087 @Override 088 public Builder timestamp(String timestamp) { 089 this.timestamp = timestamp; 090 return this; 091 } 092 093 @Override 094 public Builder error(Exception error) { 095 this.error = error; 096 return this; 097 } 098 099 @Override 100 public Builder name(String name) { 101 this.name = name; 102 return this; 103 } 104 105 public Builder dataSanitizer(DataSanitizer dataSanitizer) { 106 this.dataSanitizer = dataSanitizer; 107 return this; 108 } 109 110 public BoxAPIError build() { 111 return new BoxAPIError(this); 112 } 113 } 114 115 @Override 116 public String toString() { 117 return String.join( 118 "", 119 super.toString(), 120 String.format("\nRequest: %s", requestInfo.print(dataSanitizer)), 121 String.format("\nResponse: %s", responseInfo.print(dataSanitizer))); 122 } 123}